[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Alternative to String#to_i ?

Jeremy Lizt

8/24/2006 9:08:00 PM

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

12 Answers

Bill Kelly

8/24/2006 9:14:00 PM

0

From: "Jeremy Lizt" <jeremy.lizt@gmail.com>
>
> 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



Ara.T.Howard

8/24/2006 9:15:00 PM

0

Robin Stocker

8/24/2006 9:17:00 PM

0

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

dblack

8/24/2006 9:18:00 PM

0

dblack

8/24/2006 9:18:00 PM

0

James Gray

8/24/2006 9:19:00 PM

0

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

> 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
from :0
>> Integer("junk") rescue nil
=> nil

Hope that helps.

James Edward Gray II

Jeremy Lizt

8/24/2006 10:17:00 PM

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

Ara.T.Howard

8/24/2006 10:37:00 PM

0

dblack

8/24/2006 11:52:00 PM

0

Gennady Bystritsky

8/25/2006 12:30:00 AM

0



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

Likewise ;-)

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

Gennady.

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