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

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