why do non numbers get converted to integers in ruby?

  Kiến thức lập trình

I do this:

puts "What's your number? "
x = gets.to_i
puts x.class

If the user types in 3, that string (input) is converted to an integer. But if I type in “Charly”, that too gets converted to an integer.

What’s the best way to test whether the user typed in an number?

LEAVE A COMMENT