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 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 |
Since we're only concerned with positive integers:
def column_letter_or_index(string)
/\A\d+\z/ === string ? string.to_i : string
end
You'd probably want to .strip
the string first to prevent accidental whitespace creeping in from the command-line.
@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
would help to know a little more about the context this is being used in.