Q:

Ruby Boolean Operations

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

A:

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.)

