[Talking-Ruby] Operator precedence of ||, or, && and

Operator precedence of ||, or, && and

It’s a matter of operator precedence.

|| has a higher precedence than or.

1
2
3
4
5
6
7
8
9
a = false || true  #=> true
a #=> true

a = false or true #=> true
a #=> false

# with ()
a = (false or true) #=> true
a #=> true

&& has a higher precedence than and.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
a = :foo and nil
# => nil
a
# => :foo

a = :foo && nil
# => nil
a
# => nil

# with ()
a = (:foo and nil)
# => nil
a
# => nil

References

[1] operators - Difference between “or” and || in Ruby? - Stack Overflow - https://stackoverflow.com/questions/2083112/difference-between-or-and-in-ruby

[2] operators - Difference between “and” and && in Ruby? - Stack Overflow - https://stackoverflow.com/questions/1426826/difference-between-and-and-in-ruby

[3] Operator precedence in Ruby - https://womanonrails.com/operator-precedence-ruby