[ruby-core:123746] [Ruby Bug#21677] Integer("08") raises an ArgumentError
Issue #21677 has been reported by ben (Björn Engelmann). ---------------------------------------- Bug #21677: Integer("08") raises an ArgumentError https://bugs.ruby-lang.org/issues/21677 * Author: ben (Björn Engelmann) * Status: Open * ruby -v: 3.3.0 - 3.3.8 * Backport: 3.2: UNKNOWN, 3.3: UNKNOWN, 3.4: UNKNOWN ---------------------------------------- ruby version: tried it with 3.3.0 and 3.3.8 When running the following ruby script: p Integer("04") p Integer("05") p Integer("06") p Integer("07") p Integer("08") I get the following output: 4 5 6 7 <internal:kernel>:307:in `Integer': invalid value for Integer(): "08" (ArgumentError) from bug.rb:7:in `<main>' it seems that "08" and "09" are for some reason not parsable, while all other number "00", "01", "02", "03", "04", "05", "06", and "07" are. I would have expected the Integer-parser to have consistent behaviour, independent of the numbers that are parsed... By the way: it the same for "008", "0008" and so on. -- https://bugs.ruby-lang.org/
Issue #21677 has been updated by ufuk (Ufuk Kayserilioglu). From the docs https://docs.ruby-lang.org/en/3.4/Kernel.html#method-i-Integer:
With base zero, string object may contain leading characters to specify the actual base (radix indicator): ```ruby Integer('0100') # => 64 # Leading '0' specifies base 8. ```
So, your strings are interpreted as octal numbers (base-8) due to the leading zero, in which the value `08` is invalid. If you want to do parsing in decimal (base-10), then you need to explicitly pass the `base` argument as `10`. ---------------------------------------- Bug #21677: Integer("08") raises an ArgumentError https://bugs.ruby-lang.org/issues/21677#change-115138 * Author: ben (Björn Engelmann) * Status: Open * ruby -v: 3.3.0 - 3.3.8 * Backport: 3.2: UNKNOWN, 3.3: UNKNOWN, 3.4: UNKNOWN ---------------------------------------- ruby version: tried it with 3.3.0 and 3.3.8 When running the following ruby script: p Integer("04") p Integer("05") p Integer("06") p Integer("07") p Integer("08") I get the following output: 4 5 6 7 <internal:kernel>:307:in `Integer': invalid value for Integer(): "08" (ArgumentError) from bug.rb:7:in `<main>' it seems that "08" and "09" are for some reason not parsable, while all other number "00", "01", "02", "03", "04", "05", "06", and "07" are. I would have expected the Integer-parser to have consistent behaviour, independent of the numbers that are parsed... By the way: it the same for "008", "0008" and so on. -- https://bugs.ruby-lang.org/
Issue #21677 has been updated by nagachika (Tomoyuki Chikanaga). Status changed from Open to Rejected Thanks for the report, and for checking this. As Ufuk already stated, this is the intended behavior, and it is also clearly documented. ---------------------------------------- Bug #21677: Integer("08") raises an ArgumentError https://bugs.ruby-lang.org/issues/21677#change-115139 * Author: ben (Björn Engelmann) * Status: Rejected * ruby -v: 3.3.0 - 3.3.8 * Backport: 3.2: UNKNOWN, 3.3: UNKNOWN, 3.4: UNKNOWN ---------------------------------------- ruby version: tried it with 3.3.0 and 3.3.8 When running the following ruby script: p Integer("04") p Integer("05") p Integer("06") p Integer("07") p Integer("08") I get the following output: 4 5 6 7 <internal:kernel>:307:in `Integer': invalid value for Integer(): "08" (ArgumentError) from bug.rb:7:in `<main>' it seems that "08" and "09" are for some reason not parsable, while all other number "00", "01", "02", "03", "04", "05", "06", and "07" are. I would have expected the Integer-parser to have consistent behaviour, independent of the numbers that are parsed... By the way: it the same for "008", "0008" and so on. -- https://bugs.ruby-lang.org/
Issue #21677 has been updated by retro (Josef Šimánek). Just adding to Ufuk's great explanation and make it super clear, to fix your case, you need to call `Integer('08', 10)` and `Integer('09', 10)`. ---------------------------------------- Bug #21677: Integer("08") raises an ArgumentError https://bugs.ruby-lang.org/issues/21677#change-115140 * Author: ben (Björn Engelmann) * Status: Rejected * ruby -v: 3.3.0 - 3.3.8 * Backport: 3.2: UNKNOWN, 3.3: UNKNOWN, 3.4: UNKNOWN ---------------------------------------- ruby version: tried it with 3.3.0 and 3.3.8 When running the following ruby script: p Integer("04") p Integer("05") p Integer("06") p Integer("07") p Integer("08") I get the following output: 4 5 6 7 <internal:kernel>:307:in `Integer': invalid value for Integer(): "08" (ArgumentError) from bug.rb:7:in `<main>' it seems that "08" and "09" are for some reason not parsable, while all other number "00", "01", "02", "03", "04", "05", "06", and "07" are. I would have expected the Integer-parser to have consistent behaviour, independent of the numbers that are parsed... By the way: it the same for "008", "0008" and so on. -- https://bugs.ruby-lang.org/
Issue #21677 has been updated by vo.x (Vit Ondruch). OTOH, if the error message mentioned that it is not valid **octal** number, it would be more obvious to spot what is the problem 🤷 ---------------------------------------- Bug #21677: Integer("08") raises an ArgumentError https://bugs.ruby-lang.org/issues/21677#change-115143 * Author: ben (Björn Engelmann) * Status: Rejected * ruby -v: 3.3.0 - 3.3.8 * Backport: 3.2: UNKNOWN, 3.3: UNKNOWN, 3.4: UNKNOWN ---------------------------------------- ruby version: tried it with 3.3.0 and 3.3.8 When running the following ruby script: p Integer("04") p Integer("05") p Integer("06") p Integer("07") p Integer("08") I get the following output: 4 5 6 7 <internal:kernel>:307:in `Integer': invalid value for Integer(): "08" (ArgumentError) from bug.rb:7:in `<main>' it seems that "08" and "09" are for some reason not parsable, while all other number "00", "01", "02", "03", "04", "05", "06", and "07" are. I would have expected the Integer-parser to have consistent behaviour, independent of the numbers that are parsed... By the way: it the same for "008", "0008" and so on. -- https://bugs.ruby-lang.org/
participants (5)
-
ben -
nagachika (Tomoyuki Chikanaga) -
retro -
ufuk (Ufuk Kayserilioglu) -
vo.x (Vit Ondruch)