Feature #18410
openProposal to make inspect include underscores on numerics
Description
The documentation for inspect reads "Returns a string containing a human-readable representation of obj." but for large numerical values, while 964218442 is human-readable, 964_218_442 is a lot more readable. The language allows the use of underscores to make numbers more readable, so why should inspect not also honour this?
Happy to provide a patch if this would be an acceptable change.
Updated by mame (Yusuke Endoh) almost 3 years ago
- Related to Feature #16011: Digit grouping added
Updated by mame (Yusuke Endoh) almost 3 years ago
Thousands separators may vary depending on locales. See the discussion of #16011.
Even if we can come to an agreement on the spec, changing Integer#inspect
may have a big impact. We need to estimate the impact of the incompatibility. It would be good to first implement and try it with some projects like Rails.
Updated by duerst (Martin Dürst) almost 3 years ago
mame (Yusuke Endoh) wrote in #note-2:
Thousands separators may vary depending on locales. See the discussion of #16011.
Yes indeed. The advantage of using 964_218_442 is that a) I don't think there's actually a locale that uses this convention (but I may be wrong), and b) this is the form that Ruby uses in source.
Updated by sawa (Tsuyoshi Sawada) almost 3 years ago
In agreement with mame's comment, even if we can come to an agreement on the spec, I think it should be a property of pretty print rather than normal inspection. If there is need to make the numbers particularly readable to human, in such situations, pretty print should be used. To me, separating array or hash elements with space characters (i.e., what pretty print does) looks very similar to separating numerical digits with separators.
Changing pretty print would probably have less impact than changing inspection, and also there might be room for locale/string format configuration.
Updated by brightbits (Michael Baldry) almost 3 years ago
mame (Yusuke Endoh) wrote in #note-2:
Thousands separators may vary depending on locales. See the discussion of #16011.
Even if we can come to an agreement on the spec, changing
Integer#inspect
may have a big impact. We need to estimate the impact of the incompatibility. It would be good to first implement and try it with some projects like Rails.
As mentioned by Martin, I was proposing always using _, as you can in Ruby code, not any kind of locale specific one. Hopefully there would not be incompatibilities, as inspect is specifically meant for human readability, not by code, but I'll look in to applying this change against the Rails code.
Updated by brightbits (Michael Baldry) almost 3 years ago
sawa (Tsuyoshi Sawada) wrote in #note-4:
In agreement with mame's comment, even if we can come to an agreement on the spec, I think it should be a property of pretty print rather than normal inspection. If there is need to make the numbers particularly readable to human, in such situations, pretty print should be used. To me, separating array or hash elements with space characters (i.e., what pretty print does) looks very similar to separating numerical digits with separators.
Changing pretty print would probably have less impact than changing inspection, and also there might be room for locale/string format configuration.
I think given the output of inspect is documented to be a human readable, it's not unreasonably to expect it to be so. I agree it would be nice for pretty print to do it but that doesn't have quite the same smooth experience of just getting nice readable numbers in IRB output by default.
Saying that, is there a mechanism where IRB output gets formatted differently that isn't via inspect, something more localised to IRB? That may be a less far reaching change with the same end result
Updated by mrkn (Kenta Murata) almost 3 years ago
- Related to Feature #17339: Semantic grouping with BigDecimal#to_s added