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.

-- 
Paul Lutus
http://www.arachnoid.com