Strictly convert string to integer (or nil)

Use Integer(string)

It will raise an ArgumentError error if the string cannot convert to an integer.

Integer('5abc') #=> ArgumentError: invalid value for Integer(): "5abc"
Integer('5') #=> 5

You’d still need your number_or_nil method if you want the behavior to be that nil is returned when a string cannot be converted.

def number_or_nil(string)
  Integer(string || '')
rescue ArgumentError
  nil
end

You should be careful to rescue from a particular exception. A bare rescue (such as “rescue nil”) will rescue from any error which inherits from StandardError and may interfere with the execution of your program in ways you don’t expect. Integer() will raise an ArgumentError, so specify that.

If you’d rather not deal with exceptions and just prefer a shorter version of your number_or_nil you can take advantage of implicit return values and write it as:

def number_or_nil(string)
  num = string.to_i
  num if num.to_s == string
end

number_or_nil '5' #=> 5
number_or_nil '5abc' #=> nil

This will work the way you expect.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)