简体   繁体   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

Why is the above faulty in ruby? 为什么以上的红宝石有问题? Is there a better way to write this? 有没有更好的方法来写这个?

This is the resulting error: 这是结果错误:

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

This translates to: 这意味着:

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

You need to add parentheses: 您需要添加括号:

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

However probably this is more readable: 然而,这可能更具可读性:

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