If you do not want to use a regular expression, you can use this method:
def self.is_number(string_to_test) is_number = false # use to_f to handle float value and to_i for int string_to_compare = string_to_test.to_i.to_s string_to_compare_handle_end = string_to_test.to_i # string has to be the same if(string_to_compare == string_to_test) is_number = true end # length for fixnum in ruby size = Math.log10(string_to_compare_handle_end).to_i + 1 # size has to be the same if(size != string_to_test.length) is_number = false end is_number end
CHABANON Julien
source share