Created
August 27, 2008 14:37
-
-
Save mudge/7492 to your computer and use it in GitHub Desktop.
Methods to test whether an object is numeric or not.
This file contains hidden or 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
class Object | |
# Test if the object is an integer. | |
# | |
# "54".is_i? #=> true | |
# "54_000".is_i? #=> true | |
def is_i? | |
!!Integer(self) rescue false | |
end | |
# Test if the object is a float. | |
# | |
# "54.3".is_f? #=> true | |
# "2_000.33".is_f? #=> true | |
def is_f? | |
!!Float(self) rescue false | |
end | |
# Test if the object is either an integer or a float. | |
def is_numeric? | |
is_i? || is_f? | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment