Skip to content

Instantly share code, notes, and snippets.

@Snarp
Created June 24, 2020 19:10
Show Gist options
  • Save Snarp/07482153e9736c8096d25772b73e35b1 to your computer and use it in GitHub Desktop.
Save Snarp/07482153e9736c8096d25772b73e35b1 to your computer and use it in GitHub Desktop.
Checks whether a value is an integer, should work in Ruby 2.6 and above. (Relies on `Float()` permitting `exception: false`)
# Checks whether a value is an integer, should work in Ruby 2.6 and above.
# (Relies on `Float()` permitting `exception: false`:
# <https://rubyreferences.github.io/rubychanges/2.6.html#numeric-methods-have-exception-argument>)
def is_integer?(val)
flt = Float(val, exception: false)
return flt && flt.to_i==flt
end
def is_number?(val)
return !!Float(val, exception: false)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment