If you have a string ten
, is it possible to convert it to an integer 10
in Ruby? (maybe in rails?)
I value the developers at tryruby.org, and i
Since String#to_i
picks out only the number characters, it will not work in the way you want. There may be some Rails method related to that, but it surely will not have the method name to_i
because its behavior will conflict with the original intent of String#to_i
.
It is not only Strings
that has to_i
. NilClass
, Time
, Float
, Rational
(and perhaps some other classes) do as well.
"3".to_i #=> 3
"".to_i #=> 0
nil.to_i #=> 0
Time.now.to_i #=> 1353932622
(3.0).to_i #=> 3
Rational(10/3).to_i #=> 3