On Mon, Mar 21, 2011 at 4:09 AM, Paul Sholtz <paul.sholtz / gmail.com> wrote:
> I'm looking at some source code, to try and understand it.
> There's a variable "weight", and then there's this call:
> weight ||=3D 100
> which I take to be equivalent to:
> weight =3D weight || 100
> (i.e., performing a logical OR between "weight" and 100).

Not quite correct. there's a useful explanation here:
http://talklikeaduck.denhaven2.com/2008/04/26/x-y-redux
which (providing I'm reading Rick DeNatale's blog correctly) tells us that
(weight ||=3D 100) is expanded in Ruby as (weight || (weight =3D 100))
which *mostly* is the same as (weight =3D (weight || 100)), but might be
different (to quote Rick DeNatale) "when the left hand side is a
method call, to an accessor, or accessor-like method".

*** an extract from Rick DeNatale's blog post:

Matz explains that the real expansion of x ||=3D y is: x || x =3D y

The expectation that x ||=3D y is the same as x =3D x || y, does seem
reasonable to someone =91coming from=92 C or one of it=92s derivative
languages. As far as I can determine, C introduced the notion of
assignment operators like +=3D and -=3D. And K&R defined these assignment
operators as a shorthand for x =3D x + y, etc.

On the other hand, although C has logical operators || and && which,
like Ruby have =91short-circuit=92 evaluation, it doesn=92t allow ||=3D, or
&&=3D as assignment operators.

Since || is a =91short-circuit=92 boolean operator, the right hand operand
expression is only evaluated if the left hand operand expression
evaluates to a logically false value, i.e. either nil or false.

The way that Matz included ||=3D as an assignment operator makes perfect
sense to me. The ||=3D assignment operator reserves the short-circuit
nature of ||.

*** Rick DeNatale also points out that &&=3D has similar behaviour.