Search code examples
rubynumbersintegerargument-error

Gets.chomp input comparison returning an error


I AM USING RUBY

ERROR: comparison of Integer with String failed (ArgumentError)

puts "Age: "
    age = gets.chomp
    if 0 < age < 130

I want the programm to allow the user to input all the numbers between 0 (not included) and 130 (included). How to do it?


Solution

  • The input is a string. Try something like this

    puts "Age: "
    user_input = gets.chomp
    begin
      age = Integer(user_input)
      # your code
    rescue ArgumentError
      puts "Age must be an integer"
    end