0

In Rails (so, Ruby), I need either x and y to be true or just a to be true:

(x && y) || a

I suspect I could write

x && y || a

But I don't know how Ruby's precedence works. I did some testing and I still wasn't certain (especially with the anomaly on line 3):

true | true & false    # => true
true || true && false  # => true
true or true and false # => false #huh?

true | false & true    # => true
true || false && true  # => true
true or false and true # => true

Bonus points* for the difference between || and | and or (and the and equivalents - I believe the English versions are banned in the Ruby styleguide).

But really my question is how to write (x && y) || a without brackets.

* bonus point are only imaginary because @sawa (in the comments below) is secretly Sheldon Cooper

jcuenod
  • 55,835
  • 14
  • 65
  • 102
  • 2
    While this is not an answer for your question, it might be worth reading [this article](http://www.elpassion.com/blog/ruby-gotchas/#crayon-5506f31c4d981979786739) – Paweł Dawczak Mar 16 '15 at 15:15
  • `( )` are not brackets (in American English). They are parentheses. – sawa Mar 16 '15 at 15:19
  • @sawa Maybe upvote and accept the best answer, upvote the next that explains what the bitwise operators are. – Yaw Boakye Mar 16 '15 at 15:20
  • @YawBoakye I don't know if that is what the OP intended, but regardless of that, the OP should not write something like that. – sawa Mar 16 '15 at 15:22
  • 2
    @sawa I'm from South Africa where they're brackets and we spell colour with a "u" and have bonus points. – jcuenod Mar 16 '15 at 15:25
  • 2
    "But really my question is how to write (x && y) || a without brackets." - just write them so, `x && y || a`. `&&` is stronger than `||` so it'll evaluate first. – Sergio Tulentsev Mar 16 '15 at 15:30

2 Answers2

1

either x and y to be true or just a

Assuming I understand you correctly, I would write that as (x && y) || a, as you suggested in your question. Regardless of precedence, including the parenthesis makes it clear to others how the expression is intended to behave.

To answer your question though, yes you can omit the parenthesis in this case, as demonstrated by the test case false && false || true #=> true vs false && (false || true) => false which shows that &&'s precedence is at least equal to that that of || (it's actually higher precedence, as shown by true || false && false #=> true).

The main difference between || and | (and similarly, && and &) with booleans in Ruby is that | is a method on the boolean classes, whereas || is a language-level boolean operator. This means that || supports short-circuit evaluation, whereas | does not. Additionally, | and & both have higher precedence than && and ||, (with & having the highest precedence), as shown by false && false | true #=> false and true | false & false #=> true.

When doing boolean operations like you are now, || and && are almost always what you want.

As for the difference in precedence between || and or (or similar), that question has been asked several times already. You may want to look at Difference between "and" and && in Ruby? and Difference between "or" and || in Ruby?, as they should answer your question in that regard. (The short answer is that or and and are statement-level operators, whereas || and && are expression-level.)

Community
  • 1
  • 1
Ajedi32
  • 45,670
  • 22
  • 127
  • 172
  • I think the OP meant "either x and y to be true or just a (to be true)". There is omission. – sawa Mar 16 '15 at 15:25
  • @sawa Yeah, I thought that might be the case. Is that any different from the second case I listed? – Ajedi32 Mar 16 '15 at 15:26
  • You still don't seem to get it. Spelling out even fully would be "either [(both) x and y to be true] or [just a (to be true)]". – sawa Mar 16 '15 at 15:28
  • @sawa Ah, I get it. That's weird, I could've sworn the original question said "I need either x or y to be true or just a". One second while I edit... – Ajedi32 Mar 16 '15 at 15:31
0

The symbolic comparison operators (|| and &&) take precedence over assignment (=), while && takes precedence over ||. It's not the case with their English versions. They both have equal precedence and are lower than the assignment operator.

Yaw Boakye
  • 10,352
  • 1
  • 17
  • 25
  • 1
    `|` and `&` are bitwise operators. `|` is the bitwise OR, `&` is the bitwise AND. They behave very differently, and I think that's not what you should be using at all. – Yaw Boakye Mar 16 '15 at 15:22