Re: how to check if string is a valid number

I would even better use this:

def self.is_a_number2?(s)
   !!(s =~ /\A[+-]?\d+?(\.\d+)?\Z/)
end

Here are some benchmarks:

test with positive response ("200"):
                           user           system       total              real
is_a_number?   2.170000   0.020000   2.190000 (  2.196970)
is_a_number2?   1.750000   0.020000   1.770000 (  1.777837)

test with negative response ("2d0o0"):
                           user           system       total              real
is_a_number?   1.300000   0.000000   1.300000 (  1.302137)
is_a_number2?   0.890000   0.000000   0.890000 (  0.897787)

Last edited by knightq (2013-02-20 07:38:44)