Bug #13992
closed
Strange behaviour for kernel#integer method
Added by Ursus (Ivan Lasorsa) over 6 years ago.
Updated over 4 years ago.
Description
I tried this one with ruby 2.3.1 and ruby 2.4.1.
Integer('04')
4
Integer('05')
5
Integer('08')
ArgumentError: invalid value for Integer(): "08"
Integer('09')
ArgumentError: invalid value for Integer(): "09"
Does this makes sense?
Yes, it does. Leading zero designates octal number (base 8), which can't have digit "8" in it.
Just in plain Ruby, it is the same (but error message is more informative):
09
# SyntaxError: (irb):3: Invalid octal digit
Docu is at:
https://ruby-doc.org/core/Kernel.html#method-i-Integer
Perhaps the octal-notation could also be put into the documentation there.
And an example could be added for octal-input leading to error such as the
above. That would help explain things to newcomers who may be surprised
about the behaviour.
zverok (Victor Shepelev) wrote:
Yes, it does. Leading zero designates octal number (base 8), which can't have digit "8" in it.
Integer('09', 10)
9
This works as expected. My fault thinking that 10 should be the default base
- Status changed from Open to Closed
Also available in: Atom
PDF
Like0
Like0Like0Like0Like0Like0