Or Operator Precedence
What's the difference between ||
and or
in Ruby?
Let's look at an example to find out. First, let's start with some boolean variables:
Now, let's try the different or operators:
Cool, they seem to work as expected.
Finally, let's capture the result in a variable:
But why is c
false and not true? Operator precedence. The assignment operator (=
) takes precedence over the or
operator causing c
to be assigned to the value of a
(false
) before or
'd with b
.
Last updated