带有OR的Ruby IF语句

时间:2014-03-23 18:23:08

标签: ruby-on-rails ruby

if subject.downcase.include? product.name.downcase || body.downcase.include? product.name.downcase
  puts "111"
end

为什么上面的红宝石有问题?有没有更好的方法来写这个?

这是结果错误:

SyntaxError: (irb):7: syntax error, unexpected tIDENTIFIER, expecting keyword_then or ';' or '\n'
... body.downcase.include? product.name.downcase
...                               ^
(irb):9: syntax error, unexpected keyword_end, expecting end-of-input

1 个答案:

答案 0 :(得分:2)

这转换为:

if subject.downcase.include? (product.name.downcase || body.downcase.include? product.name.downcase)

您需要添加括号:

if subject.downcase.include?(product.name.downcase) || body.downcase.include? product.name.downcase

然而,这可能更具可读性:

if [subject, body].any? {|element| element.downcase.include? product.name.downcase}