I want to emphasize I am looking for the actual way the ||= operator is expanded by the Ruby 1.9.3 interpreter, not how it appears to be expanded based on its behavior. What I’m really hoping for is someone who has grokked the actual interpreter source, a task which I sadly am probably not up to. The only resource I have found that appears to examine this question is out of date: “A short-circuit (||=) edge case“.
The resource I mentioned above seems to suggest that the “official” expansion of x ||= y to x = x || y was either inaccurate or buggy in interpreter versions prior to 1.9. In any case, the edge case indicated seems to have been smoothed out. The resource above claims that x || x = y or x or x = y are “more accurate”. Neither of those, however, is correct, because they don’t work when x is a previously undeclared global variable:
[11:04:18][****@asha:~]$ irb
1.9.3-p194 :001 > a || a = 3
NameError: undefined local variable or method `a' for main:Object
1.9.3-p194 :002 > b or b = 3
NameError: undefined local variable or method `b' for main:Object
1.9.3-p194 :003 > c = c || 3
=> 3
So in 1.9.3, at least, the x = x || y expansion appears to be correct, as far as these examples are concerned. However, to reiterate my original point, I would really like to see some truly authoritative source resolve this question, well, authoritatively rather than anecdotally as I (and others) have done.
EDIT: This post is about the spec, read the comments to get the somewhat less ideal “implementation story”
The Ruby draft spec (PDF) section 11.4.2.3.2 defines it fairly specifically (even if fairly hard to interpret); let’s do a (theoretically somewhat loose) example with c ||= 3;
a) Evaluate the variable as a variable reference (see 11.5.4). Let V be the resulting value.
V is set to the value of
cb) Evaluate the operator-expression or the method-invocation-without-parentheses. Let W be the resulting value.
W is set to
3.c) Let OP be the assignment-operator-name of the assignment-operator.
OP is set to
||d) Let X be the operator-expression of the form V OP W.
X is set to
c || 3.e) Let I be the variable of the abbreviated-variable-assignment-expression or the abbreviated- variable-assignment-statement.
I is set to refer to
c.f) Evaluate a single-variable-assignment-expression (see 11.4.2.2.2) where its variable is I and the operator-expression is X.
c = c || 3is evaluated.g) The value of the abbreviated-variable-assignment is the resulting value of the evaluation.
The result of the assignment is
3.In other words, the expansion
c = c || 3is (excluding bugs like in pre-1.9) correct.