Search code examples
rubyuser-input

How do I check if user input is an integer in Ruby?


I am trying to loop until user inputs an integer. When user inputs a letter, the following code should print "Think of a number":

print "Think of a number "

while user_input = gets.to_i
  if user_input.is_a? Integer
    puts "your number is #{user_input}"
    break
  else
    print "Think of a number "
  end 
end 

I succeeded with my code when user inputs an integer. However when user inputs a string, the to_i method returns 0, and does not execute the else statement because it is a number.


Solution

  • The main issue with your code is String#to_i method is omnivorous.

    "0".to_i #⇒ 0
    "0.1".to_i #⇒ 0
    "foo".to_i #⇒ 0
    

    That said, user_input in your code is always integer.

    What you probably want is to accept digits only (and maybe a leading minus for negatives.) The only concise way to accept a subset of characters is a regular expression.

    # chomp to strip out trailing carriage return
    user_input = gets.chomp 
    
    if user_input =~ /\A-?\d+\z/
      ...
    

    The regular expression above means nothing save for digits with optional leading minus.


    Or, even better (credits to @Stefan)

    if gets =~ /\A-?\d+\Z/