Created
February 23, 2016 15:59
-
-
Save sparksp/a0522217f4bea189939d to your computer and use it in GitHub Desktop.
Try to cast to an integer
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
| def try_to_i(string) | |
| if string.to_i.to_s == string | |
| string.to_i | |
| else | |
| string | |
| end | |
| end |
Author
@c0nspiracy I think you have to try really hard for white space to creep in from the command-line... e.g., by quoting your input.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Since we're only concerned with positive integers:
You'd probably want to
.stripthe string first to prevent accidental whitespace creeping in from the command-line.