DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Fred has posted 10 posts at DZone. View Full User Profile

Determine If A String Is Numeric In Ruby

08.07.2010
| 485 views |
  • submit to reddit
        # Determine if a string is numeric using Float and rescue
# Revised long version from http://rosettacode.org/wiki/Determine_if_a_string_is_numeric#Ruby

def numeric?(s)
  begin
    Float(s)
  rescue
    false # not numeric
  else
    true # numeric
  end
end