Kenneth McDonald
8/26/2007 8:27:00 PM
The real problem is:
irb(main):002:0> "12,345".to_i
=> 12
Basically, Ruby converts anything that starts with an int, which is
really quite nasty in a case such as the above. I spent a lot of time
tracking down that problem.
So, I want an integer conversion function that throws an error if the
_entire_ string can't be interpreted as an integer; the Ruby analog to
the standard string-to-int conversion function in most other languages.
Thanks,
Ken
James Edward Gray II wrote:
> On Aug 25, 2007, at 10:19 PM, Kenneth McDonald wrote:
>
>> Is there an easy way to parse a string to an integer that will throw
>> an error of some sort if the string is not actually a properly
>> formatted integer? "to_i" of course is very non-strict, which is no
>> good for what I want, and I'd prefer not to write my own when it
>> seems likely that there would be some way to do this already...
>
> >> Integer("junk")
> ArgumentError: invalid value for Integer: "junk"
> from (irb):1:in `Integer'
> from (irb):1
>
> Hope that helps.
>
> James Edward Gray II
>
>