# Ruby's weird operators (||=)

**URL:** <https://www.ruby-forum.com/t/rubys-weird-operators/48319>\
**Category:** Ruby\
**Created:** [November 18, 2005, 9:18pm UTC](https://www.ruby-forum.com/t/rubys-weird-operators/48319 "2005-11-18T21:18:06Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![maggelet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a9a28c/32.png) [@maggelet](https://www.ruby-forum.com/u/maggelet)\
**Post date:** [November 18, 2005, 9:18pm UTC](https://www.ruby-forum.com/t/rubys-weird-operators/48319/1 "2005-11-18T21:18:06Z")

</div>

Hi,  
Is there an explanation somewhere of all the weird operators in ruby  
such as ||= and exactly what they do?  
Apologies if this is a faq but I’m unable to google anything.

---

<div class="post-metadata">

**Author:** ![maggelet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a9a28c/32.png) [@maggelet](https://www.ruby-forum.com/u/maggelet)\
**Post date:** [November 18, 2005, 9:24pm UTC](https://www.ruby-forum.com/t/rubys-weird-operators/48319/2 "2005-11-18T21:24:08Z")

</div>

On Sat, 2005-11-19 at 05:17 +0900, mark wrote:

> Hi,  
> Is there an explanation somewhere of all the weird operators in ruby  
> such as ||= and exactly what they do?  
> Apologies if this is a faq but I’m unable to google anything.

a = b

is completely equivalent to

a = a b

So a ||= b means a = a || b. a || b evaluates to a if a is not nil or  
false and to b otherwise. The side effect is that a is unchanged if not  
nil or equal to b otherwise.

One great use for it is default value assignment:

@a ||= a\_default\_value

@a is assigned the default value if a has no value yet (nil).

HTH,  
Guillaume.

---

<div class="post-metadata">

**Author:** ![maggelet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a9a28c/32.png) [@maggelet](https://www.ruby-forum.com/u/maggelet)\
**Post date:** [November 18, 2005, 9:24pm UTC](https://www.ruby-forum.com/t/rubys-weird-operators/48319/3 "2005-11-18T21:24:08Z")

</div>

mark wrote:

> Hi,  
> Is there an explanation somewhere of all the weird operators in ruby  
> such as ||= and exactly what they do?  
> Apologies if this is a faq but I’m unable to google anything.

Operators of that form are not methods or primitives. This

x ||= 3

is equivalent to

x = x || 3

The r.h.s. has value 3 if x is undefined or nil or false.

---

<div class="post-metadata">

**Author:** ![maggelet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a9a28c/32.png) [@maggelet](https://www.ruby-forum.com/u/maggelet)\
**Post date:** [November 18, 2005, 9:27pm UTC](https://www.ruby-forum.com/t/rubys-weird-operators/48319/4 "2005-11-18T21:27:09Z")

</div>

On Nov 18, 2005, at 2:17 PM, mark wrote:

> Hi,  
> Is there an explanation somewhere of all the weird operators in ruby  
> such as ||= and exactly what they do?  
> Apologies if this is a faq but I’m unable to google anything.

Anything of the form:

x OP= y

is translated by Ruby to:

x = x OP y

Hope that helps.

James Edward G. II

---

<div class="post-metadata">

**Author:** ![maggelet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a9a28c/32.png) [@maggelet](https://www.ruby-forum.com/u/maggelet)\
**Post date:** [November 18, 2005, 10:15pm UTC](https://www.ruby-forum.com/t/rubys-weird-operators/48319/5 "2005-11-18T22:15:26Z")

</div>

got it, that’s what I figured. Is there a list of these somewhere?  
Thanks

---

<div class="post-metadata">

**Author:** ![maggelet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a9a28c/32.png) [@maggelet](https://www.ruby-forum.com/u/maggelet)\
**Post date:** [November 18, 2005, 11:22pm UTC](https://www.ruby-forum.com/t/rubys-weird-operators/48319/6 "2005-11-18T23:22:05Z")

</div>

mark wrote:

> got it, that’s what I figured. Is there a list of these somewhere?  
> Thanks

Here you go: [http://rubygarden.org/ruby?FunnySymbolsInCode](http://rubygarden.org/ruby?FunnySymbolsInCode)

Hi, welcome to the scene, mark.

\_why
