From: Mr Magpie Date: 2007-11-27T13:19:22+09:00 Subject: Re: FEATURE SUGGESTION: Accept default value for to_f and to_i Yukihiro Matsumoto wrote: > > I think Float(astring) that raises an exception for invalid string can > do the work for you. > > |It would allow even more succinct code when, say, reading in a > |configuration value : > |delay = configuration['delay'].to_f(DEFAULT_DELAY) > > Try > > delay = Float(configuration['delay']) rescue DEFAULT_DELAY > > matz. Thankyou for replying matz. I very much like the existing to_i and to_f methods approach of never throwing exceptions as they allow things like single line method chaining without fear of exceptions in the majority of cases where 0 is the desired default. I'm merely suggesting that the error default value be customisable to distinguish bad input from valid input eg. "0".to_i and "dds".to_i both return 0 and sometimes thats fine, but other times we want to know whether the input was a valid integer or not. 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 they would be among the more performance critical of all Ruby methods, which is why I'm suggesting these changes for the C based core rather than just making my own monkey patch. Anyway, thanks for Ruby and replying to me. -- Posted via http://www.ruby-forum.com/.