# Bug with assignment?

**URL:** <https://www.ruby-forum.com/t/bug-with-assignment/114762>\
**Category:** Ruby\
**Created:** [September 17, 2007, 1:25pm UTC](https://www.ruby-forum.com/t/bug-with-assignment/114762 "2007-09-17T13:25:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mukund](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e8c25b/32.png) [@Mukund](https://www.ruby-forum.com/u/Mukund)\
**Post date:** [September 17, 2007, 1:25pm UTC](https://www.ruby-forum.com/t/bug-with-assignment/114762/1 "2007-09-17T13:25:15Z")

</div>

The snippet illustrates my question.

a={}  
b={}  
a[“alpha”]=b[“alpha”]=[]  
a[“alpha”]\<\<23  
b[“alpha”]\<\<100  
puts a[“alpha”]  
puts b[“alpha”]

Here a[“alpha”] and b[“alpha”] both contain an array of [23,100].  
Why??

If I remove the multiple assignment,  
a={}  
b={}  
a[“alpha”]=[]  
b[“alpha”]=[]  
a[“alpha”]\<\<23  
b[“alpha”]\<\<100  
puts a[“alpha”]  
puts b[“alpha”]

a[“alpha”] contains 23 and b[“alpha”] contains 100 as I expected.

---

<div class="post-metadata">

**Author:** ![Mukund](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e8c25b/32.png) [@Mukund](https://www.ruby-forum.com/u/Mukund)\
**Post date:** [September 17, 2007, 1:25pm UTC](https://www.ruby-forum.com/t/bug-with-assignment/114762/2 "2007-09-17T13:25:43Z")

</div>

On 9/16/07, Mukund [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> The snippet illustrates my question.
> 
> a={}  
> b={}  
> a[“alpha”]=b[“alpha”]=[]

You are assigning a new Array to the key “alpha” in the hash b. The  
return value from hash assignment is the object stored in the hash –  
i.e. the new Array you created. This object is then assigned to the  
key “alpha” in the hash a.

Blessings,  
TwP

---

<div class="post-metadata">

**Author:** ![Mukund](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e8c25b/32.png) [@Mukund](https://www.ruby-forum.com/u/Mukund)\
**Post date:** [September 25, 2007, 11:04pm UTC](https://www.ruby-forum.com/t/bug-with-assignment/114762/3 "2007-09-25T23:04:03Z")

</div>

On Mon, Sep 17, 2007, Mukund wrote:

> Here a[“alpha”] and b[“alpha”] both contain an array of [23,100].  
> Why??

It’s this line, right here.

> a[“alpha”]=b[“alpha”]=[]

What’s happening is the a[“alpha”] is getting the result of  
b[“alpha”]=[], which is the empty array on the far-right of the  
assignment. Both a[“alpha”] and b[“alpha”] contain a reference to the  
same array.

> If I remove the multiple assignment,  
> a[“alpha”]=[]  
> b[“alpha”]=[]

a[“alpha”] and b[“alpha”] now are both references to _different_ empty  
arrays.

Hope that helps 🙂

Ben

---

<div class="post-metadata">

**Author:** ![Mukund](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e8c25b/32.png) [@Mukund](https://www.ruby-forum.com/u/Mukund)\
**Post date:** [September 25, 2007, 11:09pm UTC](https://www.ruby-forum.com/t/bug-with-assignment/114762/4 "2007-09-25T23:09:39Z")

</div>

Mukund wrote:

> Here a[“alpha”] and b[“alpha”] both contain an array of [23,100].  
> puts b[“alpha”]
> 
> a[“alpha”] contains 23 and b[“alpha”] contains 100 as I expected.

Because, in the first case, both a[“alpha”] and b[“alpha”] refer to the  
same array. You simply have two different ways of referring to it.

In the second case, the code constructs two different arrays.
