My method uses a variable length argument list, and I would like to like to check each variable using an if-else statement. Is this possible? I'm unsure if my syntax is correct.
def buy_choice(*choice)
loop do
input = gets.chomp
if input == choice
puts "You purchased #{choice}."
break
else
puts "Input '#{input}' was not a valid choice."
end
end
end
So if I use buy_choice("sailboat", "motorboat")
, an input
of either "sailboat"
or "motorboat"
should be successful.
use Array#include? to find if an object is in the list
def buy_choice(*choices)
loop do
print 'Enter what did you buy:'
input = gets.chomp
if choices.include? input
puts "You purchased #{input}."
break
else
puts "Input '#{input}' was not a valid choice."
end
end
end
buy_choice 'abc', 'def'
Enter what did you buy:abc1
Input 'abc1' was not a valid choice.
Enter what did you buy:def1
Input 'def1' was not a valid choice.
Enter what did you buy:abc
You purchased abc.
=> nil