CRABOLO
CRABOLO

Reputation: 8793

Check to see if user input is in a certain range in Ruby

If the user types anything other than a number, and any number besides a number in range of 1 - 32, I need it to ask for input again.

It's giving me an error when I use if cut_number in 1..32

def cut_the_deck
  puts "You get to cut the deck to make it even more random!"
  puts "Type a number between 1 and 32. That's where the deck will be cut!"
    cut_number = gets.chomp
    cut_number = cut_number.to_i
      if cut_number in 1..32
        puts "Number in range"
      else
        puts "Number NOT in range"
        cut_the_deck
      end
end

Upvotes: 1

Views: 863

Answers (2)

Josh
Josh

Reputation: 5721

Use between?:

if cut_number.between?(1,32) #=> true or false

Upvotes: 3

xdazz
xdazz

Reputation: 160863

Or you could do:

if (1..32).include?(cut_number)

Upvotes: 2

Related Questions