Feature #18410
open
Proposal to make inspect include underscores on numerics
Added by brightbits (Michael Baldry) almost 3 years ago.
Updated almost 3 years ago.
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.
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.
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.
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.
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.
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
- Related to Feature #17339: Semantic grouping with BigDecimal#to_s added
Also available in: Atom
PDF
Like0
Like0Like0Like0Like0Like0Like0Like0