Casting (type conversion)
Casting to a Float
Section titled “Casting to a Float”"123.50".to_f #=> 123.5Float("123.50") #=> 123.5However, there is a difference when the string is not a valid Float:
"something".to_f #=> 0.0Float("something") # ArgumentError: invalid value for Float(): "something"Casting to a String
Section titled “Casting to a String”123.5.to_s #=> "123.5"String(123.5) #=> "123.5"Usually, String() will just call #to_s.
Methods Kernel#sprintf and String#% behave similar to C:
sprintf("%s", 123.5) #=> "123.5""%s" % 123.5 #=> "123.5""%d" % 123.5 #=> "123""%.2f" % 123.5 #=> "123.50"Casting to an Integer
Section titled “Casting to an Integer”"123.50".to_i #=> 123Integer("123.50") #=> 123A string will take the value of any integer at its start, but will not take integers from anywhere else:
"123-foo".to_i # => 123"foo-123".to_i # => 0However, there is a difference when the string is not a valid Integer:
"something".to_i #=> 0Integer("something") # ArgumentError: invalid value for Integer(): "something"Floats and Integers
Section titled “Floats and Integers”1/2 #=> 0Since we are dividing two integers, the result is an integer. To solve this problem, we need to cast at least one of those to Float:
1.0 / 2 #=> 0.51.to_f / 2 #=> 0.51 / Float(2) #=> 0.5Alternatively, fdiv may be used to return the floating point result of division without explicitly casting either operand:
1.fdiv 2 # => 0.5