Alternative to String#to_i?

Is there an alternative method to String#to_i that will return nil
instead of zero for invalid numerical strings?

Is there an alternative method to String#to_i that will return nil
instead of zero for invalid numerical strings?

Integer("123") # => 123

Ingeger("123x") # ArgumentError: invalid value for Integer: "123x"

Ingeger("123x") rescue nil # => nil

Regards,

Bill

···

From: "Jeremy Lizt" <jeremy.lizt@gmail.com>

int = Integer string

-a

···

On Fri, 25 Aug 2006, Jeremy Lizt wrote:

Is there an alternative method to String#to_i that will return nil
instead of zero for invalid numerical strings?

--
to foster inner awareness, introspection, and reasoning is more efficient than
meditation and prayer.
- h.h. the 14th dalai lama

Jeremy Lizt wrote:

Is there an alternative method to String#to_i that will return nil
instead of zero for invalid numerical strings?

You could use #Integer:

>> Integer("1")
=> 1
>> Integer("0")
=> 0
>> Integer("a")
ArgumentError: invalid value for Integer: "a"
         from (irb):3:in `Integer'
         from (irb):3

Hi --

Is there an alternative method to String#to_i that will return nil
instead of zero for invalid numerical strings?

I don't think there's one that will do that out of the box, but you
could maybe do something using Integer (the method, not the class),
which will raise an ArgumentError. Or you could do a wrapper method
like:

   def get_int(str)
     str.to_i if /\A[-+]?\d+\z/.match(str)
   end

David

···

On Fri, 25 Aug 2006, Jeremy Lizt wrote:

--
http://www.rubypowerandlight.com => Ruby/Rails training & consultancy
   ----> SEE SPECIAL DEAL FOR RUBY/RAILS USERS GROUPS! <-----
http://dablog.rubypal.com => D[avid ]A[. ]B[lack's][ Web]log
Ruby for Rails => book, Ruby for Rails
http://www.rubycentral.org => Ruby Central, Inc.

Is there an alternative method to String#to_i that will return nil
instead of zero for invalid numerical strings?

Try this:

>> Integer("junk")
ArgumentError: invalid value for Integer: "junk"
         from (irb):2:in `Integer'
         from (irb):2
>> Integer("junk") rescue nil
=> nil

Hope that helps.

James Edward Gray II

···

On Aug 24, 2006, at 4:10 PM, Jeremy Lizt wrote:
         from :0

Thanks to everyone for the quick response. The Integer suggestion works
great, but I'll point out one wrinkle that I encountered:

Integer nil # => 0

That was a small surprise. (These zeros keep popping up when you may
not expect them!) My little conversion method now works fine and looks
like this:

  def string_to_i(str)
    if str.nil? then return nil else Integer str end
  rescue nil
  end

Hi --

···

On Fri, 25 Aug 2006, ara.t.howard@noaa.gov wrote:

On Fri, 25 Aug 2006, Jeremy Lizt wrote:

Is there an alternative method to String#to_i that will return nil
instead of zero for invalid numerical strings?

int = Integer string

That will raise an ArgumentError if string is invalid for the purpose.

David

--
http://www.rubypowerandlight.com => Ruby/Rails training & consultancy
   ----> SEE SPECIAL DEAL FOR RUBY/RAILS USERS GROUPS! <-----
http://dablog.rubypal.com => D[avid ]A[. ]B[lack's][ Web]log
Ruby for Rails => book, Ruby for Rails
http://www.rubycentral.org => Ruby Central, Inc.

fyi.

harp:~ > irb
irb(main):001:0> Integer '08'
ArgumentError: invalid value for Integer: "08"
         from (irb):1:in `Integer'
         from (irb):1

i use this

···

On Fri, 25 Aug 2006, Jeremy Lizt wrote:

Thanks to everyone for the quick response. The Integer suggestion works
great, but I'll point out one wrinkle that I encountered:

Integer nil # => 0

That was a small surprise. (These zeros keep popping up when you may
not expect them!) My little conversion method now works fine and looks
like this:

def string_to_i(str)
   if str.nil? then return nil else Integer str end
rescue nil
end

#
# convert a string to an integer of any base
#
   def strtod s, opts = {}
     base = opts['base'] || opts[:base] || 10
     case base
       when 2
         s = "0b#{ s }" unless s =~ %r/^0b\d/
       when 8
         s = "0#{ s }" unless s =~ %r/^0\d/
       when 16
         s = "0x#{ s }" unless s =~ %r/^0x\d/
     end
     Integer s
   end

#
# convert a string to an integer base 10
#
   def atoi s
     strtod "#{ s }".gsub(%r/^0+/,''), :base => 10
   end

-a
--
to foster inner awareness, introspection, and reasoning is more efficient than
meditation and prayer.
- h.h. the 14th dalai lama

Hi --

···

On Fri, 25 Aug 2006, Jeremy Lizt wrote:

Thanks to everyone for the quick response. The Integer suggestion works
great, but I'll point out one wrinkle that I encountered:

Integer nil # => 0

That was a small surprise. (These zeros keep popping up when you may
not expect them!) My little conversion method now works fine and looks
like this:

def string_to_i(str)
   if str.nil? then return nil else Integer str end
rescue nil
end

I can't resist:

   def string_to_i(str)
     Integer(str) rescue nil unless str.nil?
   end

:slight_smile:

David

--
http://www.rubypowerandlight.com => Ruby/Rails training & consultancy
   ----> SEE SPECIAL DEAL FOR RUBY/RAILS USERS GROUPS! <-----
http://dablog.rubypal.com => D[avid ]A[. ]B[lack's][ Web]log
Ruby for Rails => book, Ruby for Rails
http://www.rubycentral.org => Ruby Central, Inc.

From: dblack@rubypal.com [mailto:dblack@rubypal.com] On
Behalf Of dblack@wobblini.net
Sent: Thursday, August 24, 2006 4:52 PM
To: ruby-talk ML
Subject: Re: Alternative to String#to_i ?

Hi --

> Thanks to everyone for the quick response. The Integer
suggestion works
> great, but I'll point out one wrinkle that I encountered:
>
> Integer nil # => 0
>
> That was a small surprise. (These zeros keep popping up when you may
> not expect them!) My little conversion method now works
fine and looks
> like this:
>
> def string_to_i(str)
> if str.nil? then return nil else Integer str end
> rescue nil
> end

I can't resist:

   def string_to_i(str)
     Integer(str) rescue nil unless str.nil?
   end

:slight_smile:

Likewise :wink:

def string_to_i(str)
  str and Integer(str) rescue nil
end

Gennady.

···

-----Original Message-----
On Fri, 25 Aug 2006, Jeremy Lizt wrote:

David

--
http://www.rubypowerandlight.com => Ruby/Rails training & consultancy
   ----> SEE SPECIAL DEAL FOR RUBY/RAILS USERS GROUPS! <-----
http://dablog.rubypal.com => D[avid ]A[. ]B[lack's][ Web]log
Ruby for Rails => book, Ruby for Rails
http://www.rubycentral.org => Ruby Central, Inc.

Hi --

···

On Fri, 25 Aug 2006, Gennady Bystritsky wrote:

-----Original Message-----
From: dblack@rubypal.com [mailto:dblack@rubypal.com] On
Behalf Of dblack@wobblini.net
Sent: Thursday, August 24, 2006 4:52 PM
To: ruby-talk ML
Subject: Re: Alternative to String#to_i ?

Hi --

On Fri, 25 Aug 2006, Jeremy Lizt wrote:

Thanks to everyone for the quick response. The Integer

suggestion works

great, but I'll point out one wrinkle that I encountered:

Integer nil # => 0

That was a small surprise. (These zeros keep popping up when you may
not expect them!) My little conversion method now works

fine and looks

like this:

def string_to_i(str)
   if str.nil? then return nil else Integer str end
rescue nil
end

I can't resist:

   def string_to_i(str)
     Integer(str) rescue nil unless str.nil?
   end

:slight_smile:

Likewise :wink:

def string_to_i(str)
str and Integer(str) rescue nil
end

The only reason I like mine better than yours is that mine gives nil
for false, whereas yours gives false. (Possibly not a big problem in
practice, though :slight_smile:

David

--
http://www.rubypowerandlight.com => Ruby/Rails training & consultancy
   ----> SEE SPECIAL DEAL FOR RUBY/RAILS USERS GROUPS! <-----
http://dablog.rubypal.com => D[avid ]A[. ]B[lack's][ Web]log
Ruby for Rails => book, Ruby for Rails
http://www.rubycentral.org => Ruby Central, Inc.

You win :slight_smile: -- I like yours too, acrually. Simply could not resist, as
you nicely put it ;-).

Gennady.

···

>> On Fri, 25 Aug 2006, Jeremy Lizt wrote:
>>
>>> Thanks to everyone for the quick response. The Integer
>> suggestion works
>>> great, but I'll point out one wrinkle that I encountered:
>>>
>>> Integer nil # => 0
>>>
>>> That was a small surprise. (These zeros keep popping up
when you may
>>> not expect them!) My little conversion method now works
>> fine and looks
>>> like this:
>>>
>>> def string_to_i(str)
>>> if str.nil? then return nil else Integer str end
>>> rescue nil
>>> end
>>
>> I can't resist:
>>
>> def string_to_i(str)
>> Integer(str) rescue nil unless str.nil?
>> end
>>
>> :slight_smile:
>
> Likewise :wink:
>
> def string_to_i(str)
> str and Integer(str) rescue nil
> end

The only reason I like mine better than yours is that mine gives nil
for false, whereas yours gives false. (Possibly not a big problem in
practice, though :slight_smile:

Let us get rid of the evil if/unless though

( x and Integer(str) rescue nil ) or nil

Well spent time and bandwith again :wink:

Robert

···

On 8/25/06, Gennady Bystritsky <Gennady.Bystritsky@quest.com> wrote:

> >> On Fri, 25 Aug 2006, Jeremy Lizt wrote:
> >>
> >>> Thanks to everyone for the quick response. The Integer
> >> suggestion works
> >>> great, but I'll point out one wrinkle that I encountered:
> >>>
> >>> Integer nil # => 0
> >>>
> >>> That was a small surprise. (These zeros keep popping up
> when you may
> >>> not expect them!) My little conversion method now works
> >> fine and looks
> >>> like this:
> >>>
> >>> def string_to_i(str)
> >>> if str.nil? then return nil else Integer str end
> >>> rescue nil
> >>> end
> >>
> >> I can't resist:
> >>
> >> def string_to_i(str)
> >> Integer(str) rescue nil unless str.nil?
> >> end
> >>
> >> :slight_smile:
> >
> > Likewise :wink:
> >
> > def string_to_i(str)
> > str and Integer(str) rescue nil
> > end
>
> The only reason I like mine better than yours is that mine gives nil
> for false, whereas yours gives false. (Possibly not a big problem in
> practice, though :slight_smile:

You win :slight_smile: -- I like yours too, acrually. Simply could not resist, as
you nicely put it ;-).

Gennady.

--
Deux choses sont infinies : l'univers et la bêtise humaine ; en ce qui
concerne l'univers, je n'en ai pas acquis la certitude absolue.

- Albert Einstein