On Nov 26, 2007, at 9:19 PM, Mr Magpie wrote:

> I believe exceptions are a performance drag, and these little  
> functions
> are often called thousands of times in a loop for processing input, so
> I'd prefer to avoid a method that potentially causes exceptions. I  
> think



but, unless you use #to_i exactly as it is now that is still the  
case?  with your suggestion this code

	'forty-two'.to_i(nil).abs

raises a NameError

so i think the point is that you either have

   'forty-two'.to_i.abs  # let zero be the default

    Integer 'forty-two'  # need to handle exceptions

and nothing in between because a to_i with a default that is non- 
numeric doesn't provide anything over the built-in Integer or Float.

fwiw i use this alot:

#
# convert a string to an integer of any base
#
   def strtod(s, opts = {})
     base = getopt 'base', opts, 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
#
   def atoi(s, opts = {})
     strtod("#{ s }".gsub(%r/^0+/,''), 'base' => 10)
   end



a @ http://codeforpeople.com/
--
share your knowledge.  it's a way to achieve immortality.
h.h. the 14th dalai lama