```On Wed, Mar 19, 2008 at 11:31:13AM +0900, Rick DeNatale wrote:
> On 3/18/08, Chad Perrin <perrin / apotheon.com> wrote:
> > On Tue, Mar 18, 2008 at 03:09:56PM +0900, Subbu wrote:
> >  > I came across &&= while reading some code. I know what ||= does but
> >  > not this one. Can someone explain what it does?
> >
> >
> > The other answers are not wrong, but they may not be clear.
> >
> >  Just as ||= sets the value of something if it doesn't already have one,
> >  &&= sets the value of something if it *does* already have one.
> >
> >   irb(main):001:0> a = nil
> >   => nil
> >   irb(main):002:0> b = 'foo'
> >   => "foo"
> >   irb(main):003:0> a &&= b
> >   => nil
> >   irb(main):004:0> a = 'foo'
> >   => "foo"
> >   irb(main):005:0> b = 'bar'
> >   => "bar"
> >   irb(main):006:0> a &&= b
> >   => "bar"
>
> Almost, but not quite:
>
> irb(main):001:0> a = false
> => false
> irb(main):002:0> b = 'foo'
> => "foo"
> irb(main):003:0> a &&= b
> => false
> irb(main):004:0> a
> => false
> irb(main):005:0> a ||= b
> => "foo"
> irb(main):006:0> a
> => "foo"
> irb(main):007:0>
>
>  ||= sets the value of a variable if its value if nil or false,
> &&= sets the value of something if it its value is something other
> than nil or false.

Technically more correct than my answer, but mine wasn't *incorrect*.  It
just left out the "or false" bit.  Generally, ||= and &&= are used in
cases where the variable's value may be nil, in my experience, and I
forgot to consider the rest of the story.

--
CCD CopyWrite Chad Perrin [ http://ccd.apotheon.org ]
Marvin Minsky: "It's just incredible that a trillion-synapse computer could
actually spend Saturday afternoon watching a football game."

```