I believe that I have a good answer to this question, but I wanted to make sure that Ruby-philes does not have a much better way to do this.
Basically, given the input string, I would like to convert the string to an integer where necessary, or a float where necessary. Otherwise, just return the string.
I will write my answer below, but I would like to know if there is a better way there.
Example:
to_f_or_i_or_s("0523.49") #=> 523.49
to_f_or_i_or_s("0000029") #=> 29
to_f_or_i_or_s("kittens") #=> "kittens"
source
share