# Could explain syntax "||="?

**URL:** <https://www.ruby-forum.com/t/could-explain-syntax/140427>\
**Category:** Rails\
**Created:** [June 13, 2008, 8:22pm UTC](https://www.ruby-forum.com/t/could-explain-syntax/140427 "2008-06-13T20:22:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![victordeandres](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/8c91f0/32.png) [@victordeandres](https://www.ruby-forum.com/u/victordeandres)\
**Post date:** [June 13, 2008, 8:22pm UTC](https://www.ruby-forum.com/t/could-explain-syntax/140427/1 "2008-06-13T20:22:23Z")

</div>

I don’t understand the following syntax, the “||=” and “||nil”

@country ||= (@current\_user.country || nil)

anybody could explain please ?

---

<div class="post-metadata">

**Author:** ![victordeandres](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/8c91f0/32.png) [@victordeandres](https://www.ruby-forum.com/u/victordeandres)\
**Post date:** [June 13, 2008, 8:30pm UTC](https://www.ruby-forum.com/t/could-explain-syntax/140427/2 "2008-06-13T20:30:43Z")

</div>

“@current\_user.country || nil” would return the @current\_user.country  
value if it is not false and nil if it is.

“@country ||= (@current\_user.country || nil)” would return the value  
of @country if it is not nil and the result of the above example if it  
is.

It’s like writing "@country = @country || (@current\_user.country ||  
nil) " but nicer.

On Jun 13, 7:22 pm, Victor D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![victordeandres](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/8c91f0/32.png) [@victordeandres](https://www.ruby-forum.com/u/victordeandres)\
**Post date:** [June 13, 2008, 8:26pm UTC](https://www.ruby-forum.com/t/could-explain-syntax/140427/3 "2008-06-13T20:26:01Z")

</div>

The “|| nil” is redundant in this case.

Basically, ||= is shorthand for

@country = @current\_user.country unless @country

aka, set the variable if the variable is currently ‘nil’

Jason

On Fri, Jun 13, 2008 at 2:22 PM, Victor D.

---

<div class="post-metadata">

**Author:** ![victordeandres](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/8c91f0/32.png) [@victordeandres](https://www.ruby-forum.com/u/victordeandres)\
**Post date:** [June 13, 2008, 9:31pm UTC](https://www.ruby-forum.com/t/could-explain-syntax/140427/4 "2008-06-13T21:31:37Z")

</div>

||= is pretty confusing to many people

x ||= y  
expands to  
x or x = y

It is fairly simple unless x in a hash or array, then it gets hairy.

Here is David Black’s discussion on the topic  
[http://dablog.rubypal.com/2008/3/25/a-short-circuit-edge-case](http://dablog.rubypal.com/2008/3/25/a-short-circuit-edge-case)

For a really drawn out discussion and lots of arguing go here:  
[http://groups.google.com/group/comp.lang.ruby/browse\_thread/thread/fe4fbc48e19105cd/bf7f73380e285aff?lnk=gst&q=or+equal#bf7f73380e285aff](http://groups.google.com/group/comp.lang.ruby/browse_thread/thread/fe4fbc48e19105cd/bf7f73380e285aff?lnk=gst&q=or+equal#bf7f73380e285aff)

regards

---

<div class="post-metadata">

**Author:** ![victordeandres](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/8c91f0/32.png) [@victordeandres](https://www.ruby-forum.com/u/victordeandres)\
**Post date:** [June 13, 2008, 10:36pm UTC](https://www.ruby-forum.com/t/could-explain-syntax/140427/5 "2008-06-13T22:36:16Z")

</div>

Here’s a more verbose translation:

if x.nil? or x == false then  
x = y  
else  
x  
end

(Both of those alternatives return the value in x–the first one after  
the assignment from y.)

Usually what’s meant is “give me the value in x, if there is one, but if  
x is nil, then first assign whatever’s in y to x, and _then_ give me the  
value in x”. So this method in one of my models:

def self.get\_list  
@@all\_organizations ||= self.find(:all, :select =\> ‘id, name’)  
end

Will only hit the database once. After the first time,  
@@all\_organizations won’t be nil and ruby will just return the list  
previously fetched.
