J
JZ
p '1201'.to_i(2)why result is 1 instead of 0?
p '1201'.to_i(3)correct
p '1201'.to_i(3)correct
moulon% ruby -e 'p "1A01".to_i(2); p "10B01".to_i(2)'
1
2
I found, Ruby assumes that it will extract all characters up to first
impossible character. Is seems to be wrong assumption. to_i should
return 0 or exception for all impossible conversions.
The Integer() and Float() methods are available if you want to validate
your data, but in 1.8.x, Integer() does not take a base as an argument.
It is unfortunate that the interface for these methods is not
self-documenting.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.