Ruby Type Conversion
to_i, to_f, to_s, to_a and to_sym convert values. Integer("42") raises an error on bad text, while "abc".to_i quietly gives 0.
Example
puts "42".to_i + 1
puts "2.5".to_f * 2
puts 7.to_s + "!"
puts "abc".to_i
puts Integer("12")
puts :sym.to_sResult
43 5.0 7! 0 12 sym
Exercise
Text to integer.
"7".Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.