Project

General

Profile

Bug #13992

Strange behaviour for kernel#integer method

Added by Ursus (Ivan Lasorsa) almost 2 years ago. Updated about 1 month ago.

Status:
Closed
Priority:
Normal
Assignee:
-
Target version:
-
ruby -v:
2.4.1
[ruby-core:83192]

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?

History

Updated by zverok (Victor Shepelev) almost 2 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) almost 2 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) almost 2 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) almost 2 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

#5

Updated by jeremyevans0 (Jeremy Evans) about 1 month ago

  • Status changed from Open to Closed

Also available in: Atom PDF