Since this has recently come up in a work context, I would like to take this opportunity to remind everyone to always specify the expected base when trying to convert strings to integers using Integer(value)
, because as opposed to String#to_i
, the Integer(…)
constructor does honor prefixes like 0
for octal, 0x
for hexadecimal, etc.
Example:
Integer("0462011540000077") #=> 21029460115519
Integer("0461990540002606") #=> ArgumentError
"0462011540000077".to_i #=> 462011540000077
"0461990540002606".to_i #=> 461990540002606
# now, specify the base explicitly:
Integer("0461990540002606", 10) #=> 461990540002606
The same thing happened with JavaScript’s parseInt
and bit lots of people, so: cover your bases.