From: Giles Bowkett Date: 2006-11-18T04:31:14+09:00 Subject: Re: ruby equivalent PHP function is_numeric? On 11/17/06, Gavin Kistner wrote: > From: jason.dusek@gmail.com [mailto:jason.dusek@gmail.com] On > > It really is too bad that String.to_f does not throw an exception - or > > at least return NaN - when the string is not a number. > > Float( mystring ) > does raise an exception, however. hmm, I'm new to Ruby too, so I tried this in irb. irb(main):001:0> muppet = "3.14" => "3.14" irb(main):002:0> muppet.to_f => 3.14 irb(main):003:0> muppet => "3.14" irb(main):004:0> penguin = "banana" => "banana" irb(main):005:0> penguin.to_f => 0.0 irb(main):006:0> Float(muppet) => 3.14 irb(main):007:0> Float(penguin) ArgumentError: invalid value for Float(): "banana" from (irb):7:in `Float' from (irb):7 from :0 irb(main):008:0> exit I think String#to_f should probably throw an exception too. actually, I think whatever the behavior of the two methods is, it should probably be the same in either case. otherwise you end up with the kind of API where you have thirty different methods of doing the same thing, but only two of them work, and of the two that work, neither of them work all the time. it's like, you have Perl's "more than one way to do it" and Python's "only one way to do it" and you have to be careful about letting these creatures mate, it's kind of a mad scientist situation, you could end up with a monster like, "there's more than one way to do it, but only one of them actually works, and which one it is varies unpredictably." -- Giles Bowkett http://www.gilesgoatboy.org