On 2006-11-17 18:11:10 +0100, Paul Lutus <nospam / nosite.zzz> said: > Josselin wrote: > >> After reading completely my Ruby book, I cannot find a function >> equivalent to the PHP is_numeric? to test a String >> >> myString.is_numeric? (check only digits and dot character) => >> return true or false >> >> does it exist ? or it's more complicated than that ? (need to build a >> regex... ?) > > ------------------------------------ > #!/usr/bin/ruby -w > > def is_numeric?(s) > return (s.to_s =~ /^\d+(\.\d+|\d*)$/)?true:false > end > > puts is_numeric?(1.2345) > puts is_numeric?(12345678987654321) > puts is_numeric?(0) > puts is_numeric?(0.0) > puts is_numeric?(".001") > puts is_numeric?(123435.12345) > puts is_numeric?("123435.") > ------------------------------------ > > Output: > > true > true > true > true > false > true > false > > An extension to this function would handle float exponents and associated > characters. thanks Paul... I see why there is no such function (I am translating PHP -> Ruby) , too many cases better using a regexp....