On Sun, Jan 17, 2010 at 11:31 AM, John M. [email protected]
n -= 1
This code is (presumably) showing that it is expensive to calculate the
string “99 bottles of beer on the wall\n98 bottles … \n no more
beer” and so what it does instead is calculates the string the first
the method is called, but then not again after that.
The first time the method is called, @expensive will be nil, which
to false. So in the boolean expression, @expensive || "99 bottles … "
the string will be returned. The ||= command can be thought of as
@expensive || ( @expensive = "99 bottles … " )
In this case, if @expensive does not have a value, it will assign the
to it. After that, however, it will have the value, and so the string
not be assigned to it. This is useful, because the code that generates
string is expensive to perform, and so we will never perform it unless
know we need it, and after it has been performed, it should not have to