更简单地说"anwer ="这个"或回答="那个"

jun*_*nky 0 ruby expression function

可能重复:
优雅的链接'或'用于Ruby中相同变量的测试

answer="this" or answer = "that" 很罗嗦

我希望能够使用更像的表达式,answer =("this" or that")但我知道我不能使用该表达式作为or完成,然后将(true或false)结果与answer进行比较,这不是我想要的.更像是=〜[a/c],但我需要插值.

Chu*_*uck 7

我假设你的意思是==(平等比较)而不是=(赋值).如果是这种情况,根据具体情况,您可以执行以下操作之一:

if [this, that].include? answer

# or #

case answer
when this, that
  # do something
end
Run Code Online (Sandbox Code Playgroud)

当你想要检查多个选项时,后者更好,而当你只有一组你感兴趣的选项时前者更具可读性.(我通常会将选项放在一个选项中)命名变量,所以它会更像if right_answers.include? answer.这样它读得清楚,易于维护.)