# ||= in an expression

**URL:** <https://www.ruby-forum.com/t/in-an-expression/100127>\
**Category:** Ruby\
**Created:** [July 27, 2006, 10:12am UTC](https://www.ruby-forum.com/t/in-an-expression/100127 "2006-07-27T10:12:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rejoc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/cab0a1/32.png) [@rejoc](https://www.ruby-forum.com/u/rejoc)\
**Post date:** [July 27, 2006, 10:12am UTC](https://www.ruby-forum.com/t/in-an-expression/100127/1 "2006-07-27T10:12:37Z")

</div>

Hello,

I new to Ruby and curious about this language I do use (yeet).

I was browsing through some libraries/modules and I found a line looking  
like

a[:i] ||= “a string”

I could not find this syntax in any manual online (and googling “||=” is  
not of much help ☹ )

Can anyone tell me what it means

TiA

---

<div class="post-metadata">

**Author:** ![rejoc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/cab0a1/32.png) [@rejoc](https://www.ruby-forum.com/u/rejoc)\
**Post date:** [July 27, 2006, 10:18am UTC](https://www.ruby-forum.com/t/in-an-expression/100127/2 "2006-07-27T10:18:50Z")

</div>

On Jul 27, 2006, at 3:10 AM, rejoc wrote:

> =" is not of much help ☹ )
> 
> Can anyone tell me what it means
> 
> TiA

Consider:

“some string” || “a string”

versus:

nil || “a string”

The former returns “some string”, but the latter returns “a string”.  
Apply that to the form you described and it basically means that if a  
[:i] is nil then it assigns “a string”, otherwise it leaves it alone.

- Jake McArthur

---

<div class="post-metadata">

**Author:** ![rejoc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/cab0a1/32.png) [@rejoc](https://www.ruby-forum.com/u/rejoc)\
**Post date:** [July 27, 2006, 10:19am UTC](https://www.ruby-forum.com/t/in-an-expression/100127/3 "2006-07-27T10:19:12Z")

</div>

On 27/07/06, rejoc [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> not of much help ☹ )
> 
> Can anyone tell me what it means
> 
> TiA

In Ruby

nil || anObject

will return anObject and

anotherObject || anObject

will return anotherObject (assuming it’s value isn’t nil)

The expression

a[:i] ||= “a string”

is equivalent to

a[:i] = a[:i] || “a string”

(sort of like ‘num+= 1’ is the same as ‘num = num + 1’)If a[:i] is not  
nil then it will remain unchanged. If it is nil then it will be set to  
“a string”.

Farrel

---

<div class="post-metadata">

**Author:** ![rejoc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/cab0a1/32.png) [@rejoc](https://www.ruby-forum.com/u/rejoc)\
**Post date:** [July 27, 2006, 10:22am UTC](https://www.ruby-forum.com/t/in-an-expression/100127/4 "2006-07-27T10:22:40Z")

</div>

On 27-Jul-06, at 4:10 AM, rejoc wrote:

> =" is not of much help ☹ )
> 
> Can anyone tell me what it means

it will return the value for a[:i] if a value exists, if not, it will  
set a[:i] to the value “a string”.

–  
Jeremy T.  
removed\_email\_address@domain.invalid

“One serious obstacle to the adoption of good programming languages  
is the notion that everything has to be sacrificed for speed. In  
computer languages as in life, speed kills.” – Mike Vanier

---

<div class="post-metadata">

**Author:** ![rejoc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/cab0a1/32.png) [@rejoc](https://www.ruby-forum.com/u/rejoc)\
**Post date:** [July 27, 2006, 10:36am UTC](https://www.ruby-forum.com/t/in-an-expression/100127/5 "2006-07-27T10:36:41Z")

</div>

On 7/27/06, rejoc [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I could not find this syntax in any manual online (and googling “||=” is  
> not of much help ☹ )
> 
> Can anyone tell me what it means
> 
> TiA

It is very useful as

var ||= expr

is a shortcut for

var = expr unless defined? var and var

which means if var is defined and evaluates to true it is left alone  
however if var is not defined or evaluates to false it is assigned expr.  
That means also that  
var ||= expr  
will overwrite nil _and_ false values in var, a fact which seems to be  
overseen sometimes.

Cheers  
Robert

–  
Deux choses sont infinies : l’univers et la bÃªtise humaine ; en ce qui  
concerne l’univers, je n’en ai pas acquis la certitude absolue.

- Albert Einstein

---

<div class="post-metadata">

**Author:** ![rejoc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/cab0a1/32.png) [@rejoc](https://www.ruby-forum.com/u/rejoc)\
**Post date:** [July 27, 2006, 10:46am UTC](https://www.ruby-forum.com/t/in-an-expression/100127/6 "2006-07-27T10:46:50Z")

</div>

Farrel L. a écrit :

> will return anotherObject (assuming it’s value isn’t nil)  
> nil then it will remain unchanged. If it is nil then it will be set to  
> “a string”.
> 
> Farrel

Thanks a lot.
