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 Or Numeric Represents A Number In Ruby

08.07.2010
| 375 views |
  • submit to reddit
        # Determine if a String or Numeric represents a number
# From http://www.pixellatedvisions.com/2009/06/22/string-is-a-number#comment-67

def is_numeric?
  self.is_a?(Numeric) or (self.is_a?(String) and !!Float(self))
rescue
  false
end