string to int

P

Phillip Curry

[Note: parts of this message were removed to make it a legal post.]

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? The method to_i
returns 0 if the string is not an integer, which isn't hugely helpful, as I
want to be able to distinguish between the integer 0 and non-integer
strings.
thanks
 
J

Jesús Gabriel y Galán

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? =A0The method to= _i
returns 0 if the string is not an integer, which isn't hugely helpful, as= I
want to be able to distinguish between the integer 0 and non-integer
strings.
thanks

Try this:

irb(main):002:0> Integer("12")
=3D> 12
irb(main):003:0> Integer("fdfdf")
ArgumentError: invalid value for Integer: "fdfdf"
from (irb):3:in `Integer'
from (irb):3
from :0
irb(main):005:0> Integer("12ijk")
ArgumentError: invalid value for Integer: "12ijk"
from (irb):5:in `Integer'
from (irb):5
from :0
irb(main):006:0> "12ijk".to_i
=3D> 12

But check the last example: to_i would have returned 12 ignoring the
rest of the string, while Integer will raise an exception. Don't know
if this fits your criteria.

Jesus.
 
P

Phillip Curry

Yeah that's exactly what I was looking for. Thanks for the help, I'm prett=
y
new to ruby and still exploring all this stuff.
 
R

Rick DeNatale

Jes=FAs Gabriel y Gal=E1n:
=85and if you=92d rather get nil, you can try rescuing the exception inli= ne:
=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

Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale
 
J

James Edward Gray II

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? The method =
to_i returns 0 if the string is not an integer, which isn't hugely =
helpful, as I want to be able to distinguish between the integer 0 and =
non-integer strings.

I see you already have your answer, but just in case it helps I've =
written a blog post about some handy conversion methods, like Integer():

http://blog.grayproductions.net/articles/conversion_methods

James Edward Gray II=
 

Ask a Question

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.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top