# What does &&= do?

**URL:** <https://www.ruby-forum.com/t/what-does-do/132238>\
**Category:** Ruby\
**Created:** [March 18, 2008, 7:10am UTC](https://www.ruby-forum.com/t/what-does-do/132238 "2008-03-18T07:10:40Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![subbu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a88e57/32.png) [@subbu](https://www.ruby-forum.com/u/subbu)\
**Post date:** [March 18, 2008, 7:10am UTC](https://www.ruby-forum.com/t/what-does-do/132238/1 "2008-03-18T07:10:40Z")

</div>

I came across &&= while reading some code. I know what ||= does but  
not this one. Can someone explain what it does?

Thanks much  
-subbu

---

<div class="post-metadata">

**Author:** ![subbu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a88e57/32.png) [@subbu](https://www.ruby-forum.com/u/subbu)\
**Post date:** [March 18, 2008, 7:30am UTC](https://www.ruby-forum.com/t/what-does-do/132238/2 "2008-03-18T07:30:03Z")

</div>

On Tue, Mar 18, 2008 at 7:09 AM, Subbu [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I came across &&= while reading some code. I know what ||= does but  
> not this one. Can someone explain what it does?
> 
> Thanks much  
> -subbu

a &&=b is a shortcut for a = a && b

---

<div class="post-metadata">

**Author:** ![subbu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a88e57/32.png) [@subbu](https://www.ruby-forum.com/u/subbu)\
**Post date:** [March 18, 2008, 10:21am UTC](https://www.ruby-forum.com/t/what-does-do/132238/3 "2008-03-18T10:21:50Z")

</div>

Subbu wrote:

> I came across &&= while reading some code. I know what ||= does but  
> not this one. Can someone explain what it does?

self assignment  
Examples:  
foo += 12

Syntax:  
expr op= expr # left hand side must be assignable.

This form evaluated as expr = expr op expr. But right hand side  
expression  
evaluated once. op can be one of:  
+, -, \*, /, %, \*\*, &, |, ^, \<\<, \>\>, &&, ||

There may be no space between operators and =. \>

RTFM?

---

<div class="post-metadata">

**Author:** ![subbu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a88e57/32.png) [@subbu](https://www.ruby-forum.com/u/subbu)\
**Post date:** [March 19, 2008, 2:28am UTC](https://www.ruby-forum.com/t/what-does-do/132238/4 "2008-03-19T02:28:25Z")

</div>

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”

Is that clear enough?

---

<div class="post-metadata">

**Author:** ![subbu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a88e57/32.png) [@subbu](https://www.ruby-forum.com/u/subbu)\
**Post date:** [March 19, 2008, 7:45am UTC](https://www.ruby-forum.com/t/what-does-do/132238/5 "2008-03-19T07:45:40Z")

</div>

On Wed, Mar 19, 2008 at 11:31:13AM +0900, Rick DeNatale wrote:

> > irb(main):006:0\> a &&= b  
> > 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.

---

<div class="post-metadata">

**Author:** ![subbu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a88e57/32.png) [@subbu](https://www.ruby-forum.com/u/subbu)\
**Post date:** [March 19, 2008, 3:32am UTC](https://www.ruby-forum.com/t/what-does-do/132238/6 "2008-03-19T03:32:17Z")

</div>

On 3/18/08, Chad P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> irb(main):001:0\> a = nil  
> =\> “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.

Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![subbu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a88e57/32.png) [@subbu](https://www.ruby-forum.com/u/subbu)\
**Post date:** [March 19, 2008, 6:11pm UTC](https://www.ruby-forum.com/t/what-does-do/132238/7 "2008-03-19T18:11:23Z")

</div>

On Tue, 18 Mar 2008 20:26:22 -0500, Chad P. wrote:

> =\> nil
> 
> Is that clear enough?

Consider the following practical use I gave in my solution for Ruby Q.  
144:

DAYNAMES=%w[Sun Mon Tue Wed Thu Fri Sat]  
DAYNAME=%r{Sun|Mon|Tue|Wed|Thu|Fri|Sat}  
TIME=%r{[0-9]+}

#find a single day, or a hyphenated range of days  
clause.scan(/(#{DAYNAME})(?:(?=\s)|$)|(#{DAYNAME})-(#{DAYNAME})/)   
do |single,start,finish|

#convert data in single, start, and finish only if they’re not nil  
#though the &&= is not strictly necessary since Array#index() handles  
#nil properly, some conversions like obj.to\_i will fail badly  
#if obj is nil  
single &&= DAYNAMES.index(single)  
start &&= DAYNAMES.index(start)  
finish &&= DAYNAMES.index(finish)

#do something with the ones that are not nil

end

---

<div class="post-metadata">

**Author:** ![subbu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a88e57/32.png) [@subbu](https://www.ruby-forum.com/u/subbu)\
**Post date:** [March 22, 2008, 2:30pm UTC](https://www.ruby-forum.com/t/what-does-do/132238/8 "2008-03-22T14:30:43Z")

</div>

On Mar 19, 10:08 pm, Ken B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > irb(main):001:0\> a = nil  
> > =\> “bar”  
> > #find a single day, or a hyphenated range of days
> 
> #do something with the ones that are not nil
> 
> end
> 
> –  
> Ken (Chanoch) Bloom. PhD candidate. Linguistic Cognition Laboratory.  
> Department of Computer Science. Illinois Institute of Technology.[http://www.iit.edu/~kbloom1/](http://www.iit.edu/~kbloom1/)

Thanks all for the detailed explanations. Really nice of you.
