Skip to content

Instantly share code, notes, and snippets.

@foomin10
Last active August 29, 2015 14:10
Show Gist options
  • Save foomin10/ed80342acd79b3a16677 to your computer and use it in GitHub Desktop.
Save foomin10/ed80342acd79b3a16677 to your computer and use it in GitHub Desktop.
[ruby] 文字列から Integer や Float に変換できるか調べる ref: http://qiita.com/MizuiroFolder/items/69d55d6d423ae6ef27c4
"5foo".to_i #=> 5
"foo5".to_i #=> 0
Integer "5foo" #~> invalid value for Integer(): "5foo" (ArgumentError)
class String
def int_valid?
Integer(self)
true
rescue ArgumentError
false
end
def float_valid?
Float(self)
true
rescue ArgumentError
false
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment