Type Conversion
to_i, to_f, to_s, to_a and Integer() convert values. Integer() raises on bad input.
Example
puts "42abc".to_i, "3.5".to_f, 65.to_s, "abc".to_i
puts Integer("12") + 1
begin
Integer("x")
rescue ArgumentError
puts "bad number"
endResult
42 3.5 65 0 13 bad number
Exercise
String to int.
"5".Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.