From: Gennady Bystritsky Date: 2006-08-26T00:18:51+09:00 Subject: Re: Alternative to String#to_i ? > >> On Fri, 25 Aug 2006, Jeremy Lizt wrote: > >> > >>> 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 :-) You win :-) -- I like yours too, acrually. Simply could not resist, as you nicely put it ;-). Gennady.