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

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
irb(main):005:0> Integer("12ijk")
ArgumentError: invalid value for Integer: "12ijk"
  from (irb):5:in `Integer'
  from (irb):5
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.

···

On Tue, Jan 19, 2010 at 11:13 AM, Phillip Curry <philfo@gmail.com> wrote:
  from :0
  from :0

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():

  Gray Soft / Not Found

James Edward Gray II

···

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

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 <jgabrielygalan@gmail.com>

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? 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.

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?

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

…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 11:13 AM, Phillip Curry <philfo@gmail.com> wrote:

--
We have things like protected properties. We have abstract methods.
We have all this stuff that your computer science teacher told you
you should be using. I don’t care about this crap at all.
                                            [Rasmus Lerdorf on PHP]

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.

···

On Tue, Jan 19, 2010 at 5:40 AM, Shot (Piotr Szotkowski) <shot@hot.pl> wrote:

Jesús Gabriel y Galán:

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")
=> 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

…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

--
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

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

···

On Tue, Jan 19, 2010 at 3:37 PM, Rick DeNatale <rick.denatale@gmail.com> wrote:

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

Yep.

···

On Tue, Jan 19, 2010 at 10:52 AM, Xavier Noria <fxn@hashref.com> wrote:

On Tue, Jan 19, 2010 at 3:37 PM, Rick DeNatale <rick.denatale@gmail.com> wrote:

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

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

--
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