Created
June 24, 2020 19:10
-
-
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`)
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
# 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