如何在 Ruby 中编写复杂的多行 if 条件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21417527/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 06:22:39  来源:igfitidea点击:

How do I write a complex multi-line if condition in Ruby?

rubyif-statementconditionalconditional-statements

提问by Chloe

How do I write this multi-line, complex condition if statement in Ruby?

如何在 Ruby 中编写这个多行、复杂的 if 语句?

  if ( (aa != nil && self.prop1 == aa.decrypt)   
      || (bb != nil && self.prop2 == bb.decrypt)  
    ) && (self.id.nil? || self.id != id) 
    return true
  end

I'm getting Syntax error; unexpected tOROP.

我得到Syntax error; unexpected tOROP

In Java, I could write

在 Java 中,我可以写

if (
     ( (aa != null && aa.prop1.equals(aa.decrypt()))
     || (bb != null && bb.prop2.equals(bb.decrypt()))
     )
     && (this.id != id)
   ) {

   return true;
}

回答by Kyle Smith

The short answer is the operator needs to be at the end of the line in order to tell Ruby to continue reading the next line as part of the statement, so this would work:

简短的回答是操作符需要在行尾才能告诉 Ruby 继续阅读下一行作为语句的一部分,所以这会起作用:

if ( (aa != nil && self.prop1 == aa.decrypt) ||
   (bb != nil && self.prop2 == bb.decrypt) ) &&
   (self.id.nil? || self.id != id)
  return true
end

That being said, you can probably reduce the logic by throwing exceptions based on input values, and removing some redundant checks (I'm making some jumps here about what your variables will look like, but you get the idea.)

话虽如此,您可能可以通过根据输入值抛出异常并删除一些冗余检查来减少逻辑(我在这里对您的变量的外观进行了一些跳转,但您明白了。)

raise 'aa must support decrypt' unless aa.respond_to? :decrypt
raise 'bb must support decrypt' unless bb.respond_to? :decrypt
if prop1 == aa.decrypt || prop2 == bb.decrypt
  if self.id != id
    return true
  end
end

回答by DiegoSalazar

You need to escape whitespace with the backslash character, it's ugly but you need it if you want to split conditions to multiple lines. Either that or leave the boolean operator on the previous line. So either of these will work:

您需要使用反斜杠字符转义空格,这很丑陋,但如果您想将条件拆分为多行,则需要它。要么将布尔运算符留在上一行。因此,其中任何一个都将起作用:

if ( (aa != nil && self.prop1 == aa.decrypt)\
      || (bb != nil && self.prop2 == bb.decrypt)\
    ) && (self.id.nil? || self.id != id) 
    return true
  end

or:

或者:

if ( (aa != nil && self.prop1 == aa.decrypt) || 
      (bb != nil && self.prop2 == bb.decrypt)) &&
    (self.id.nil? || self.id != id) 
    return true
  end

Personally I usually decide to put each or all conditions in a method that self documents what's being decided:

就我个人而言,我通常决定将每个或所有条件放在一种自我记录正在决定的内容的方法中:

def everythings_cool?
  ( (aa != nil && self.prop1 == aa.decrypt) || 
          (bb != nil && self.prop2 == bb.decrypt)) &&
        (self.id.nil? || self.id != id) 
end

then:

然后:

if everythings_cool?
  # do stuff