Skip to content

Instantly share code, notes, and snippets.

@VasylShevchenko
Created November 22, 2019 09:21
Show Gist options
  • Save VasylShevchenko/aac5b9fd3a3a88c313499006d1622e45 to your computer and use it in GitHub Desktop.
Save VasylShevchenko/aac5b9fd3a3a88c313499006d1622e45 to your computer and use it in GitHub Desktop.
https://medium.com/launch-school/number-validation-with-regex-ruby-393954e46797
def number?(obj)
obj = obj.to_s unless obj.is_a? String
/\A[+-]?\d+(\.[\d]+)?\z/.match(obj)
end
/\A[+-]?\d+(\.\d+)?\z/
/ start of the regex
\A start of the string to be matched
[+-]? zero or one of '+' or'-'
\d+ one or more of digit
(\.\d+)? zero or one of 'one dot and 'one or more of digit''
\z end of the string to be matched
/ end of the regex
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment