Last active
August 29, 2015 14:10
-
-
Save foomin10/ed80342acd79b3a16677 to your computer and use it in GitHub Desktop.
[ruby] 文字列から Integer や Float に変換できるか調べる ref: http://qiita.com/MizuiroFolder/items/69d55d6d423ae6ef27c4
This file contains 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
"5foo".to_i #=> 5 | |
"foo5".to_i #=> 0 | |
Integer "5foo" #~> invalid value for Integer(): "5foo" (ArgumentError) |
This file contains 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
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