Created
June 11, 2009 15:43
-
-
Save TwP/127996 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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