# Assignments

**URL:** <https://www.ruby-forum.com/t/assignments/172956>\
**Category:** Ruby\
**Created:** [July 30, 2009, 3:24pm UTC](https://www.ruby-forum.com/t/assignments/172956 "2009-07-30T15:24:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ftf3k3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/b9bd4f/32.png) [@ftf3k3](https://www.ruby-forum.com/u/ftf3k3)\
**Post date:** [July 30, 2009, 3:24pm UTC](https://www.ruby-forum.com/t/assignments/172956/1 "2009-07-30T15:24:41Z")

</div>

Hello folks, I have a question about Ruby 1.8.7.

After doing:

a = b = 1  
a += 1

a returns 2 and b returns 1.

But if I try:

a = b = []  
a \<\< 1

both a and b returns [1]. Why?

Regards

---

<div class="post-metadata">

**Author:** ![ftf3k3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/b9bd4f/32.png) [@ftf3k3](https://www.ruby-forum.com/u/ftf3k3)\
**Post date:** [July 30, 2009, 3:34pm UTC](https://www.ruby-forum.com/t/assignments/172956/2 "2009-07-30T15:34:26Z")

</div>

On Thu, Jul 30, 2009 at 9:25 AM, Ftf 3k3[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hello folks, I have a question about Ruby 1.8.7.

This question actually applies to all Ruby versions.

It’s important to remember that variables in Ruby are just labels for  
references. From that, here are some hints:

> After doing:
> 
> a = b = 1  
> a += 1

This translates to a = a + 1  
Because Fixnums are immediate values in Ruby, Fixnum#+ does not modify  
the original object, it instead returns a new one.

So even though a and b originally pointed to the same object, you are  
reassigning a to a new object when you do +=

> a returns 2 and b returns 1.
> 
> But if I try:
> 
> a = b = []  
> a \<\< 1
> 
> both a and b returns [1]. Why?

a and b are pointing to the same object. Array#\<\< is a destructive  
operator, and modifies the object itself. Since you do not re-assign  
a, it still points at the original object, as does b.

If you wanted to do this in a nondestructive way, you could do:

a += [1]

which translates to

a = a + [1]

which would create a new array and reassign a to it.

But generally speaking, except for with simple values like numbers,  
boolean states, and things of that like, you don’t want to do:

a = b = some\_obj

unless you intentionally want two labels point to that single object.  
Hope this helps.

-greg

---

<div class="post-metadata">

**Author:** ![ftf3k3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/b9bd4f/32.png) [@ftf3k3](https://www.ruby-forum.com/u/ftf3k3)\
**Post date:** [July 30, 2009, 3:40pm UTC](https://www.ruby-forum.com/t/assignments/172956/3 "2009-07-30T15:40:59Z")

</div>

Gregory B. wrote:

> Hope this helps.
> 
> -greg

Thanks that’s very helpful!

---

<div class="post-metadata">

**Author:** ![ftf3k3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/b9bd4f/32.png) [@ftf3k3](https://www.ruby-forum.com/u/ftf3k3)\
**Post date:** [July 30, 2009, 3:50pm UTC](https://www.ruby-forum.com/t/assignments/172956/4 "2009-07-30T15:50:50Z")

</div>

Ftf 3k3 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> a = b = []  
> a \<\< 1
> 
> both a and b returns [1]. Why?

Because a\<\<1 doesn’t modify a. It sends the message \<\< with the  
argument 1 to the object referenced by a. But it’s always the same  
object that’s referenced by a, and of course, it’s the same that is  
referenced by b too.

In the case of a+=1, you are misled by the syntax (too much syntax  
gives the cancer of the semicolon). a+=1 is actually a = a+1, which  
is sending the message + with the argument 1 to the obejct referenced  
by a. That object will behave according to its method, and will  
return a new object. This new object will now be referenced by a.
