# What does "||=" mean?

**URL:** <https://www.ruby-forum.com/t/what-does-mean/134734>\
**Category:** Rails\
**Created:** [April 14, 2008, 3:44pm UTC](https://www.ruby-forum.com/t/what-does-mean/134734 "2008-04-14T15:44:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [April 14, 2008, 3:44pm UTC](https://www.ruby-forum.com/t/what-does-mean/134734/1 "2008-04-14T15:44:55Z")

</div>

Hi there !

I don’t quite understand the following method (taken from RESTful  
authentication) and can’t google the problem since the Goog won’t accept  
the search term in question ("||=").

The method:

def current\_user  
@current\_user ||= (login\_from\_session || login\_from\_basic\_auth ||  
login\_from\_cookie || :false)  
end

My question:

I know “||” means a boolean “or”, but what does “||=” mean?  
Can you translate the above method into a sentence?

Thank you very much for your help!  
Tom

---

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [April 14, 2008, 3:52pm UTC](https://www.ruby-forum.com/t/what-does-mean/134734/2 "2008-04-14T15:52:27Z")

</div>

It’s an idiom for @current\_user = @current\_user || …

… Helps to keep it DRY

---

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [April 14, 2008, 3:59pm UTC](https://www.ruby-forum.com/t/what-does-mean/134734/3 "2008-04-14T15:59:09Z")

</div>

Thanks very much, idleFingers !

Btw, I like your nickname, seems like you’re Mr DRY in person… 🙂  
Tom

---

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [April 14, 2008, 3:56pm UTC](https://www.ruby-forum.com/t/what-does-mean/134734/4 "2008-04-14T15:56:10Z")

</div>

@current\_user ||= some\_expression means use @current\_user if it’s  
non-nil or  
true; otherwise, assign some\_expression to @current\_user and then use  
@current\_user.

---

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [April 14, 2008, 4:00pm UTC](https://www.ruby-forum.com/t/what-does-mean/134734/5 "2008-04-14T16:00:53Z")

</div>

Hi –

On Mon, 14 Apr 2008, idleFingers wrote:

> It’s an idiom for @current\_user = @current\_user || …

The most accurate representation of how it expands is:

@current\_user || @current\_user = x

(See ruby-talk for recent discussions of why this is, also:  
[http://dablog.rubypal.com/2008/3/25/a-short-circuit-edge-case](http://dablog.rubypal.com/2008/3/25/a-short-circuit-edge-case))

David

–  
Rails training from David A. Black and Ruby Power and Light:  
ADVANCING WITH RAILS April 14-17 New York City  
INTRO TO RAILS June 9-12 Berlin  
ADVANCING WITH RAILS June 16-19 Berlin  
See [http://www.rubypal.com](http://www.rubypal.com) for details and updates!
