繁体   English   中英

带有OR的Ruby IF语句

[英]Ruby IF statement with OR

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

这意味着:

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}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM