Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

  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.

In Java, I could write

if (      ( (aa != null && aa.prop1.equals(aa.decrypt()))      || (bb != null && bb.prop2.equals(bb.decrypt()))      )      && (this.id != id)    ) {     return true; } 
like image 515
Chloe Avatar asked Jan 28 '14 21:01

Chloe


People also ask

How do you write if condition in Ruby on Rails?

Ruby if...else Statement Notice Ruby uses elsif, not else if nor elif. Executes code if the conditional is true. If the conditional is not true, code specified in the else clause is executed. An if expression's conditional is separated from code by the reserved word then, a newline, or a semicolon.

How do you break out of if statements in Ruby?

In Ruby, we use a break statement to break the execution of the loop in the program. It is mostly used in while loop, where value is printed till the condition, is true, then break statement terminates the loop. In examples, break statement used with if statement. By using break statement the execution will be stopped.

How do you write or condition in Ruby?

In Ruby, “or” keyword returns the logical disjunction of its two operands. The condition becomes true if both the operands are true. It returns “true” when any one condition/expression is “true” and returns “false” only when all of them are “false”.

How many conditions can we specify in an IF block?

Technically only 1 condition is evaluated inside an if , what is ascually happening is 1 condition gets evaluated and its result is evaluated with the next and so on... There is no defined limit to the length of a boolean expression; likely, you will run into memory problems at some point.


2 Answers

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:

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 
like image 54
Kyle Smith Avatar answered Sep 30 '22 07:09

Kyle Smith


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 
like image 33
DiegoSalazar Avatar answered Sep 30 '22 07:09

DiegoSalazar