Steve Tuckner <SAT / MULTITECH.com> writes:

> How does that x ||= 1 work (for us newbies)?

    x ||= expr

is shorthand for

    x = x || expr

So x is assigned the value in 'x', unless 'x' is false or nil, in
which case 'expr' is assigned. If x is neither nil nor false, then
'expr' is not evaluated 'expr'.

In a hash, h[key] is nil if the key is missing from the hash, so

   h[key] ||= expr

assigns expr to h[key] only is h[key] is not set.

Of course, the new hash defaulting mechanism coming along soon will
make this kind of hack unnecessary.


Regards


Dave