On 10/30/07, 7stud -- <bbxx789_05ss / yahoo.com> wrote:
> Martin DeMello wrote:
> >
> > a ||= b is a common ruby idiom for "set a to b only if it isn't
> > already defined" -
> >
> > it expands to a = a || b.
>
> Nope.  What do you think the following will output:
>
> h = Hash.new('hi')
>
> h['a'] = h['a'] || 10
> puts h['a']
>
> Now, how about the following?  The same output as above?
>
> h['a'] ||= 10
> puts h['a']

Yes, and IRB confirms it (ruby 1.8.6). AFAIK, all the a o= b operators
expand immediately to a = a o b.

martin