String to int

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

On Tue, Jan 19, 2010 at 11:13 AM, Phillip C. [email protected]
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? 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

Try this:

irb(main):002:0> Integer(“12”)
=> 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
=> 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.

Yeah that’s exactly what I was looking for. Thanks for the help, I’m
pretty
new to ruby and still exploring all this stuff.

2010/1/19 Jesús Gabriel y Galán [email protected]

Jesús Gabriel y Galán:

On Tue, Jan 19, 2010 at 11:13 AM, Phillip C. [email protected] 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:

from (irb):5
from :0

…and if you’d rather get nil, you can try rescuing the exception inline:

Integer(‘12’)
=> 12
Integer(‘fdfdf’) rescue nil
=> nil
Integer(‘12ijk’) rescue nil
=> nil

— Shot

On Tue, Jan 19, 2010 at 5:40 AM, Shot (Piotr S.) [email protected]
wrote:

=> 12

…and if you’d rather get nil, you can try rescuing the exception inline:

Integer(‘12’)
=> 12
Integer(‘fdfdf’) rescue nil
=> nil
Integer(‘12ijk’) rescue nil
=> nil

However you need to be aware of issues like this:

ruby-1.8.6-p383 > Integer(“0xFF”)
=> 255
ruby-1.8.6-p383 > Integer(“033”)
=> 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.


Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: Rick DeNatale - Developer - IBM | LinkedIn

On Tue, Jan 19, 2010 at 3:37 PM, Rick DeNatale [email protected]
wrote:

def safe_string_to_int(string)
  if /^\d+$/.match(string)

\A and \z would be better surely for the same price.

On Jan 19, 2010, at 4:13 AM, Phillip C. 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? 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 G. II

On Tue, Jan 19, 2010 at 10:52 AM, Xavier N. [email protected] wrote:

On Tue, Jan 19, 2010 at 3:37 PM, Rick DeNatale [email protected] wrote:

def safe_string_to_int(string)
if /^\d+$/.match(string)

\A and \z would be better surely for the same price.

Yep.

Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: Rick DeNatale - Developer - IBM | LinkedIn