Bug #13992
closedStrange behaviour for kernel#integer method
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?
Updated by zverok (Victor Shepelev) over 6 years ago
Yes, it does. Leading zero designates octal number (base 8), which can't have digit "8" in it.
Updated by zverok (Victor Shepelev) over 6 years ago
Just in plain Ruby, it is the same (but error message is more informative):
09
# SyntaxError: (irb):3: Invalid octal digit
Updated by shevegen (Robert A. Heiler) over 6 years ago
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.
Updated by Ursus (Ivan Lasorsa) over 6 years ago
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
Updated by jeremyevans0 (Jeremy Evans) over 4 years ago
- Status changed from Open to Closed