# ||= operator

**URL:** <https://www.ruby-forum.com/t/operator/82194>\
**Category:** Rails\
**Created:** [January 17, 2007, 11:16pm UTC](https://www.ruby-forum.com/t/operator/82194 "2007-01-17T23:16:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nima](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/9dc877/32.png) [@Nima](https://www.ruby-forum.com/u/Nima)\
**Post date:** [January 17, 2007, 11:16pm UTC](https://www.ruby-forum.com/t/operator/82194/1 "2007-01-17T23:16:22Z")

</div>

Hey all

I read in Agile Web D. with Rails that you can set a value like  
this,  
@my\_time ||= Time.now

What does the operator mean, and what is the difference between using  
||= and = ?

---

<div class="post-metadata">

**Author:** ![Nima](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/9dc877/32.png) [@Nima](https://www.ruby-forum.com/u/Nima)\
**Post date:** [January 17, 2007, 11:18pm UTC](https://www.ruby-forum.com/t/operator/82194/2 "2007-01-17T23:18:02Z")

</div>

Nima wrote:

> What does the operator mean, and what is the difference between using  
> ||= and = ?

Much like ‘x += 1’ is a shortcut for ‘x = x + 1’, ‘x ||= 1’ is a  
shortcut for ‘x = x || 1’.

The significance of this is that the || method doesn’t simply return a  
boolean value; it checks the first argument, returns it if it evaluates  
to ‘true’ (i.e. is something other than false or nil), and otherwise  
returns the second argument. So it’s commonly used as a shortcut for an  
if/then statement, checking if the first argument is nil (or false). So  
the following are all equivalent:

if @account  
@account  
else  
Account.find(session[:account\_id])  
end

is the same as:

@account = @account || Account.find(session[:account\_id])

is the same as:

@account ||= Account.find(session[:account\_id])

Hope this helps!

---

<div class="post-metadata">

**Author:** ![Nima](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/9dc877/32.png) [@Nima](https://www.ruby-forum.com/u/Nima)\
**Post date:** [January 17, 2007, 11:25pm UTC](https://www.ruby-forum.com/t/operator/82194/3 "2007-01-17T23:25:29Z")

</div>

Definitely does, thanks Chris!

---

<div class="post-metadata">

**Author:** ![Nima](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/9dc877/32.png) [@Nima](https://www.ruby-forum.com/u/Nima)\
**Post date:** [January 18, 2007, 12:45am UTC](https://www.ruby-forum.com/t/operator/82194/4 "2007-01-18T00:45:02Z")

</div>

> I read in Agile Web D. with Rails that you can set a value like  
> this,  
> @my\_time ||= Time.now
> 
> What does the operator mean, and what is the difference between using  
> ||= and = ?

foo = 5 # foo now equals 5  
foo ||= 6 # foo still equals 5

||= is saying if the left hand side is nil, then set the left hand side to  
the right hand side, otherwise just leave well enough alone.

-philip

---

<div class="post-metadata">

**Author:** ![Nima](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/9dc877/32.png) [@Nima](https://www.ruby-forum.com/u/Nima)\
**Post date:** [January 17, 2007, 11:36pm UTC](https://www.ruby-forum.com/t/operator/82194/5 "2007-01-17T23:36:54Z")

</div>

Hi Nima,

On 17 Jan 2007, at 10.16 pm, Nima wrote:

> What does the operator mean, and what is the difference between using  
> ||= and = ?

||= is called the conditional assignment. It is essentially syntactic  
sugar for (i.e. a shorter way of writing):

@my\_time = @my\_time || Time.now

The way this works is based on the behaviour of the OR (||) part of  
the assignment, and the general behaviour of statements in Ruby:  
statements always return something.

So, let’s look at the following statement:

true || false

if you try this in irb you’ll see the following:

> > true || false  
> > =\> true

irb tells us that Ruby returns false for the expression true ||  
false. If we remember our boolean logic, we know this is the right  
answer.

However, where things start to get tricky is something like this:

> > true || “cheese”  
> > =\> true

> > false || “cheese”  
> > =\> “cheese”

> > “cheese” || “beef”  
> > =\> “cheese”

Notice how Ruby returns the value “cheese” in the second example  
above, and the first value in the third example (Ruby shortcuts the  
or, and knowing that true or anything is true, just returns the first  
value).

Now, as Ruby isn’t statically typed, it lets us put anything on  
either side of the || operator. Now, the way it turns a standard  
object into true or false in order to evaluate the logic is as follows:

if the object is nil, or false, then it is represented as false.  
if the object has any other value whatsoever, then it is represented  
as true.

So, knowing this, we can see how Ruby sees the string “cheese” as  
being true.

Now, returning back to the original question, we can see that:

@answer = false || “cheese”

would assign the value of “cheese” to answer.

Now, if we wrote:

@answer = @answer || “cheese”

then we get two possible behaviours:

1. if @answer has a value, then @answer is set to that value  
(basically this doesn’t do anything)
2. if @answer doesn’t exist, it is nil, which means that @answer will  
be set to “cheese” following the behaviour we’ve just seen.

Hope this helps,

Sam A.

* * *

> **[Sam Aaron - A Communicative Programmer](http://sam.aaron.name)**
>
> Dr. Sam Aaron is a software architect and computational thinker with a deep fascination with the notion of communicative programming.
