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?

From: “Jeremy Lizt” [email protected]

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

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

-a

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 –

On Fri, 25 Aug 2006, [email protected] 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

Hi –

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?

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

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

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 G. II

On Fri, 25 Aug 2006, Jeremy Lizt wrote:

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

Hi –

On Fri, 25 Aug 2006, Jeremy Lizt wrote:

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

Hi –

On Fri, 25 Aug 2006, Gennady B. wrote:

fine and looks
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

not expect them!) My little conversion method now works
def string_to_i(str)

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.

On 8/25/06, Gennady B. [email protected] wrote:

when you may

end

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

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

:slight_smile:

Likewise :wink:

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

Gennady.