From: ara.t.howard@... Date: 2006-11-18T03:14:41+09:00 Subject: Re: ruby equivalent PHP function is_numeric? On Sat, 18 Nov 2006, 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... ?) > > thanks > > joss harp:~ > cat a.rb def is_numeric?(n) Float n rescue false end args = 1.2345, 12345678987654321, 0, 0.0, ".001", 123435.12345, "123435.", "1.50130937545297e+68", "a", "a42", "42a", 1_2_3.42, "1_2_3.42", "_1_2_3_.42_", "__1__2_3_.42__" args.each{|a| puts "is_numeric?(#{ a.inspect }) #=> #{ is_numeric? a }"} harp:~ > ruby a.rb is_numeric?(1.2345) #=> 1.2345 is_numeric?(12345678987654321) #=> 1.23456789876543e+16 is_numeric?(0) #=> 0.0 is_numeric?(0.0) #=> 0.0 is_numeric?(".001") #=> 0.001 is_numeric?(123435.12345) #=> 123435.12345 is_numeric?("123435.") #=> 123435.0 is_numeric?("1.50130937545297e+68") #=> 1.50130937545297e+68 is_numeric?("a") #=> false is_numeric?("a42") #=> false is_numeric?("42a") #=> false is_numeric?(123.42) #=> 123.42 is_numeric?("1_2_3.42") #=> 123.42 is_numeric?("_1_2_3_.42_") #=> false is_numeric?("__1__2_3_.42__") #=> false -a -- my religion is very simple. my religion is kindness. -- the dalai lama