Created
June 25, 2012 21:28
-
-
Save danmayer/2991367 to your computer and use it in GitHub Desktop.
Ruby number parsing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>> Integer('1001') | |
=> 1001 | |
>> Integer('1001a') | |
ArgumentError: invalid value for Integer: "1001a" | |
from (irb):26:in `Integer' | |
from (irb):26 | |
#note this is very odd, so is '10a100'.to_i | |
>> '1001a'.to_i | |
=> 1001 | |
>> Float('1001.0') | |
=> 1001.0 | |
>> Float('1001.0a') | |
ArgumentError: invalid value for Float(): "1001.0a" | |
from (irb):29:in `Float' | |
from (irb):29 | |
#better more normal to_i usage | |
>> Integer('10') | |
=> 10 | |
>> Integer('10a') | |
ArgumentError: invalid value for Integer: "10a" | |
from (irb):33:in `Integer' | |
from (irb):33 | |
>> Integer('10') | |
=> 10 | |
>> Integer('bat') | |
ArgumentError: invalid value for Integer: "bat" | |
from (irb):35:in `Integer' | |
from (irb):35 | |
>> "bat".to_i | |
=> 0 | |
>> "10".to_i | |
=> 10 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment