如何使用带有可变长度参数列表的 if-else 语句?

How can I use an if-else statement with a variable length argument list?

我的方法使用可变长度参数列表,我想使用 if-else 语句检查每个变量。这可能吗?我不确定我的语法是否正确。

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

因此,如果我使用 buy_choice("sailboat", "motorboat")"sailboat""motorboat" 中的 input 应该会成功。

使用数组#include?查找对象是否在列表中

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