# Operator |=

**URL:** <https://www.ruby-forum.com/t/operator/130626>\
**Category:** Ruby\
**Created:** [February 29, 2008, 11:15pm UTC](https://www.ruby-forum.com/t/operator/130626 "2008-02-29T23:15:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![chinnakaruppan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c4cdca/32.png) [@chinnakaruppan](https://www.ruby-forum.com/u/chinnakaruppan)\
**Post date:** [February 29, 2008, 11:15pm UTC](https://www.ruby-forum.com/t/operator/130626/1 "2008-02-29T23:15:25Z")

</div>

Hi,  
I am trying to under this operator |=.Has anybody got some documentation  
/ online link to explain this…

THnks  
Chinna

---

<div class="post-metadata">

**Author:** ![chinnakaruppan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c4cdca/32.png) [@chinnakaruppan](https://www.ruby-forum.com/u/chinnakaruppan)\
**Post date:** [February 29, 2008, 11:26pm UTC](https://www.ruby-forum.com/t/operator/130626/2 "2008-02-29T23:26:49Z")

</div>

On Feb 29, 2008, at 5:15 PM, Chinna K. wrote:

> Hi,  
> I am trying to under this operator |=.Has anybody got some  
> documentation  
> / online link to explain this…
> 
> THnks  
> Chinna

That’s one of the assignment operators:

a |= b

is the same as

a = a | b

with all the regular message sending semantics. Look for the | method  
of the class of a.

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![chinnakaruppan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c4cdca/32.png) [@chinnakaruppan](https://www.ruby-forum.com/u/chinnakaruppan)\
**Post date:** [March 1, 2008, 12:26am UTC](https://www.ruby-forum.com/t/operator/130626/3 "2008-03-01T00:26:29Z")

</div>

Chinna K. wrote:

> Hi,  
> I am trying to under this operator |=.Has anybody got some documentation  
> / online link to explain this…
> 
> THnks  
> Chinna

The number 10 written in binary format is:

8’s 4’s 2’s 1’s

* * *

1 0 1 0

The number 11 written in binary format is:

8’s 4’s 2’s 1’s

* * *

1 0 1 1

Assume 1 is equal to true and 0 is equal to false. Now OR each column  
together:

## 1 0 1 0 1 0 1 1 OR

```
       <--result?

```

In the first column(the leftmost column) you have (true OR true), which  
is true, i.e you write 1 for the result:

## 1 0 1 0 1 0 1 1 OR

1 \<–result?

In the second column, you have (false OR false), which is false, i.e you  
write a 0 for the result:

## 1 0 1 0 1 0 1 1 OR

1 0 \<–result?

In the third column, you have (true OR true), which is true:

## 1 0 1 0 1 0 1 1 OR

1 0 1 \<–result?

In the last column, you have (false OR true) which is true:

## 1 0 1 0 1 0 1 1 OR

1 0 1 1 \<–result?

What number is that?

8’s 4’s 2’s 1’s

* * *

1 0 1 1

1x8 + 0x4 + 1x2 + 1x1 = 11. So 10 | 11 is equal to 11.

The syntax a |= b is similar to the syntax a += 1. a += 1 is equivalent  
to a = a + 1. So a |= b is equivalent to a = a | b. From the above you  
should now know how to OR two numbers together: convert the numbers to  
binary format and then OR the columns together.

---

<div class="post-metadata">

**Author:** ![chinnakaruppan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c4cdca/32.png) [@chinnakaruppan](https://www.ruby-forum.com/u/chinnakaruppan)\
**Post date:** [February 29, 2008, 11:55pm UTC](https://www.ruby-forum.com/t/operator/130626/4 "2008-02-29T23:55:46Z")

</div>

x |= y is equivalent to x = x | y

---

<div class="post-metadata">

**Author:** ![chinnakaruppan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c4cdca/32.png) [@chinnakaruppan](https://www.ruby-forum.com/u/chinnakaruppan)\
**Post date:** [March 1, 2008, 1:01am UTC](https://www.ruby-forum.com/t/operator/130626/5 "2008-03-01T01:01:53Z")

</div>

On Sat, 01 Mar 2008 08:26:21 +0900, 7stud – [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> From the above you  
> should now know how to OR two numbers together: convert the numbers to  
> binary format and then OR the columns together.

Note that in ruby | is just a method. For integers it gives the result  
of  
bitwise or, but other classes define it differently. For instance,  
Array  
defines it to return set union, so [1] | [2] is [1,2].
