Skip to content

Instantly share code, notes, and snippets.

@TwP
Created June 11, 2009 15:43
Show Gist options
  • Save TwP/127996 to your computer and use it in GitHub Desktop.
Save TwP/127996 to your computer and use it in GitHub Desktop.
# Convert a string to a numeric value
class String
# Convert the string into a numeric value. If the string does not
# represent a valid Integer or Float, then the string itself is returned.
#
# "10".numeric #=> 10
# "1.0".numeric #=> 1.0
# "foo".numeric #=> "foo"
#
def numeric
Integer(self) rescue (Float(self) rescue self)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment