From: Mr Magpie Date: 2007-11-27T14:46:50+09:00 Subject: Re: FEATURE SUGGESTION: Accept default value for to_f and to_i ara.t.howard wrote: > 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 Of course if you were chaining class-specific methods like abs you would have to use a default supporting that method. > > 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. > Yes, that is the choice at present. The benefits my suggestion provides are : 1) allows an application specific default (of any type) to be supplied, reducing code required. 2) allows bad input to be unambigously detected, which (can distinguish "fds".to_i from "0".to_i) 3) because the result of to_i always evaluates to true, you can't do num.to_i ? 'valid int' : 'invalid int' but with my sugestion you could do num.to_i(false) ? 'valid int' : 'invalid int' 4) would be a miniscule change to the existing optimised C unlike some monkey patch I could do 5) would avoid performance-sapping exceptions 6) would avoid expensive regular expressions 7) as a default parameter, wouldn't affect existing code. I don't think any other approach satisfies all of the above. Thanks for your reply and the code examples. Regards, magpie -- Posted via http://www.ruby-forum.com/.