Skip to content

Instantly share code, notes, and snippets.

@ryancastro
Forked from anonymous/test a number in Ruby
Created July 25, 2013 15:45
Show Gist options
  • Save ryancastro/6081063 to your computer and use it in GitHub Desktop.
Save ryancastro/6081063 to your computer and use it in GitHub Desktop.
Test to see if a number is a number in Ruby. Useful because: "I am a string".to_i returns 0, which is an an Integer.... so my string will test positive for a number.
def isInt?(number)
true if Integer(number) rescue false
end
isInt?("13")
result = true if Integer("12") rescue false # true
result = true if Integer("test") rescue false #false
result = true if Float("test") rescue false #false
result = true if Float(13.2) rescue false #true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment