Skip to content

Instantly share code, notes, and snippets.

@mudge
Created August 27, 2008 14:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mudge/7492 to your computer and use it in GitHub Desktop.
Save mudge/7492 to your computer and use it in GitHub Desktop.
Methods to test whether an object is numeric or not.
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