Alternative to String#to_i ?

J

Jeremy Lizt

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

Bill Kelly

From: "Jeremy Lizt said:
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
 
R

Robin Stocker

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

You could use #Integer:
ArgumentError: invalid value for Integer: "a"
from (irb):3:in `Integer'
from (irb):3
 
D

dblack

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

--
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
http://www.manning.com/black => book, Ruby for Rails
http://www.rubycentral.org => Ruby Central, Inc.
 
J

James Edward Gray II

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

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

Hope that helps.

James Edward Gray II
 
J

Jeremy Lizt

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
 
A

ara.t.howard

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

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

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

dblack

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

:)


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
http://www.manning.com/black => book, Ruby for Rails
http://www.rubycentral.org => Ruby Central, Inc.
 
G

Gennady Bystritsky

=20
-----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
Thanks 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
=20
I can't resist:
=20
def string_to_i(str)
Integer(str) rescue nil unless str.nil?
end
=20
:)

Likewise ;-)

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

Gennady.
=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
 
D

dblack

Hi --

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

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


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
http://www.manning.com/black => book, Ruby for Rails
http://www.rubycentral.org => Ruby Central, Inc.
 
G

Gennady Bystritsky

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

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

Gennady.
 

Ask a Question

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.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top