My question was triggered by this discussion on SO, which did not lead to an answer that would really explain the issue. I am "rewriting" it here in a slightly different way, because I want to make it more clear what the real problem is and therefore hope to get an answer here.
Consider the following two Ruby expressions:
1 * a - 3
1 && a = 3
From the Ruby precedence table, we know that of the operators mentioned here, *
has the highest precedence, followed by -
, then by &&
and finally by =
.
The expressions don't have parenthesis, but - as we can verify in irb, providing a suitable value for a
in the first case - they are evaluated as if the bracketing were written as (1*a) - 3
, respectively 1 && (a=3)
.
The first one is easy to understand, since *
binds stronger than -
.
The second one can't be explained in this way. &&
binds stronger than =
, so if precedence only would matter, the interpretation should be (1 && a) = 3
.
Associativity (=
is right-associative and -
is left-associative) can't explain the effect either, because associativity is only important if we have several operators of the same kind (such as x-y-z
or x=y=z
).
There must be some special rule in the assignment operator, which I did not find in the docs I checked in particular the docs for assignment and syntax.
Could someone point out, where this special behaviour of the assignment operator is documented? Or did I miss / misunderstand something here?