J
Jeremy Lizt
Is there an alternative method to String#to_i that will return nil
instead of zero for invalid numerical strings?
instead of zero for invalid numerical strings?
From: "Jeremy Lizt said: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?
Jeremy said: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?
int = Integer string
Is there an alternative method to String#to_i that will return nil
instead of zero for invalid numerical strings?
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
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
-----Original Message-----
From: (e-mail address removed) [mailto:[email protected]] On=20
Behalf Of (e-mail address removed)
Sent: Thursday, August 24, 2006 4:52 PM
To: ruby-talk ML
Subject: Re: Alternative to String#to_i ?
=20
Hi --
=20
On Fri, 25 Aug 2006, Jeremy Lizt wrote:
=20=20Thanks to everyone for the quick response. The Integer=20 suggestion works
great, but I'll point out one wrinkle that I encountered:
Integer nil # =3D> 0
That was a small surprise. (These zeros keep popping up when you may
not expect them!) My little conversion method now works=20 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:
=20
def string_to_i(str)
Integer(str) rescue nil unless str.nil?
end
=20
=20
=20
David
=20
--=20
http://www.rubypowerandlight.com =3D> Ruby/Rails training & = consultancy
----> SEE SPECIAL DEAL FOR RUBY/RAILS USERS GROUPS! <-----
http://dablog.rubypal.com =3D> D[avid ]A[. ]B[lack's][ Web]log
http://www.manning.com/black =3D> book, Ruby for Rails
http://www.rubycentral.org =3D> Ruby Central, Inc.
=20
=20
-----Original Message-----
From: (e-mail address removed) [mailto:[email protected]] On
Behalf Of (e-mail address removed)
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
Likewise ;-)
def string_to_i(str)
str and Integer(str) rescue nil
end
=20
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
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.