簡體   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