简体   繁体   中英

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}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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