```On Tue, Jan 19, 2010 at 5:40 AM, Shot (Piotr Szotkowski) <shot / hot.pl> wrot=
e:
> Jes=FAs Gabriel y Gal=E1n:
>
>> On Tue, Jan 19, 2010 at 11:13 AM, Phillip Curry <philfo / gmail.com> wrote=
:
>
>>> Is there a nice way to convert a string to an integer if it is in
>>> fact an integer, and to return nil or even an error if it's not?
>
>> Try this:
>
>> irb(main):002:0> Integer("12")
>> =3D> 12
>> irb(main):003:0> Integer("fdfdf")
>> ArgumentError: invalid value for Integer: "fdfdf"
>> =A0 =A0 =A0 from (irb):3:in `Integer'
>> =A0 =A0 =A0 from (irb):3
>> =A0 =A0 =A0 from :0
>> irb(main):005:0> Integer("12ijk")
>> ArgumentError: invalid value for Integer: "12ijk"
>> =A0 =A0 =A0 from (irb):5:in `Integer'
>> =A0 =A0 =A0 from (irb):5
>> =A0 =A0 =A0 from :0
>
> =85and if you=92d rather get nil, you can try rescuing the exception inli=
ne:
>
>>> Integer('12')
> =3D> 12
>>> Integer('fdfdf') rescue nil
> =3D> nil
>>> Integer('12ijk') rescue nil
> =3D> nil

However you need to be aware of issues like this:

ruby-1.8.6-p383 > Integer("0xFF")
=3D> 255
ruby-1.8.6-p383 > Integer("033")
=3D> 27
ruby-1.8.6-p383 > Integer("082")
ArgumentError: invalid value for Integer: "082"
from (irb):5:in `Integer'
from (irb):5

It should be obvious, but what's happening is that the Kernel#Integer
method treats an initial 0 as indication of a radix, with 0x giving
base 16, 0b base 2, and 0 alone base 8.

If this is an issue, then I'd recommend validating arbitrary strings
using a regex before conversion to an integer. Something like:

def safe_string_to_int(string)
if /^\d+\$/.match(string)
string.to_i(10)
else
nil
end
end

There are probably better ways to write this, but I think that this
form may be clearer for a newbie to understand.

--=20
Rick DeNatale