From: Yukihiro Matsumoto Date: 2007-11-27T11:32:18+09:00 Subject: Re: FEATURE SUGGESTION: Accept default value for to_f and to_i Hi, In message "Re: FEATURE SUGGESTION: Accept default value for to_f and to_i" on Tue, 27 Nov 2007 11:15:19 +0900, Mr Magpie writes: |I suggest that to_i() and to_f() have an optional parameter added with |the default value of 0 (for backwards compatibility). | |This would allow code like | |if astring.to_f(nil) | # valid, so use it |else | # not a valid float, nil was returned, so handle error |end 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.