# Assignment question

**URL:** <https://www.ruby-forum.com/t/assignment-question/193099>\
**Category:** Ruby\
**Created:** [August 11, 2010, 7:39am UTC](https://www.ruby-forum.com/t/assignment-question/193099 "2010-08-11T07:39:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![marlon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8c91f0/32.png) [@marlon](https://www.ruby-forum.com/u/marlon)\
**Post date:** [August 11, 2010, 7:39am UTC](https://www.ruby-forum.com/t/assignment-question/193099/1 "2010-08-11T07:39:09Z")

</div>

#Run this code

x=[2,6,5,9]  
y = x  
puts x  
puts  
puts y  
puts

## x=[1,2,3,4] puts x puts puts y

#this also

x=[2,6,5,9]  
y = x  
puts x  
puts  
puts y  
puts

## x.pop puts x puts puts y

I understand the first code, y was still pointing to [2,6,5,9]  
but isn’t the second code similar? shouldn’t y still be [2,6,5,9] ?

Or is the method pop an exemption in this case?

Beginner here. Thanks everyone!

---

<div class="post-metadata">

**Author:** ![marlon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8c91f0/32.png) [@marlon](https://www.ruby-forum.com/u/marlon)\
**Post date:** [August 11, 2010, 8:53am UTC](https://www.ruby-forum.com/t/assignment-question/193099/2 "2010-08-11T08:53:39Z")

</div>

Kaye Ng wrote:

> #Run this code
> 
> x=[2,6,5,9]  
> y = x  
> puts x  
> puts  
> puts y  
> puts
> 
> ## x=[1,2,3,4] puts x puts puts y
> 
> #this also
> 
> x=[2,6,5,9]  
> y = x  
> puts x  
> puts  
> puts y  
> puts
> 
> ## x.pop puts x puts puts y
> 
> I understand the first code, y was still pointing to [2,6,5,9]  
> but isn’t the second code similar? shouldn’t y still be [2,6,5,9] ?
> 
> Or is the method pop an exemption in this case?
> 
> Beginner here. Thanks everyone!

In the second snippet x.pop removes the last  
element, leaving x=[2,6,5], and y points to  
the modified array.

HTH gfb

---

<div class="post-metadata">

**Author:** ![marlon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8c91f0/32.png) [@marlon](https://www.ruby-forum.com/u/marlon)\
**Post date:** [August 11, 2010, 12:38pm UTC](https://www.ruby-forum.com/t/assignment-question/193099/3 "2010-08-11T12:38:51Z")

</div>

Kaye Ng wrote:

> #Run this code
> 
> x=[2,6,5,9]  
> y = x

x and y are pointing to the same array

> x=[1,2,3,4]

Now x is pointing to a different array (look at x.object\_id and  
y.object\_id)

> x=[2,6,5,9]  
> y = x

x and y are pointing to the same array

> x.pop

x and y are still pointing to the same array. You modified this array by  
popping an element off it.

So you need to remember:

- Every value in Ruby is an object _reference_
- Most objects in Ruby are mutable, i.e. their internal state can  
change.

> > a = “hello”  
> > =\> “hello”
> 
> > b = a  
> > =\> “hello”
> 
> > a.upcase!  
> > =\> “HELLO”
> 
> > a  
> > =\> “HELLO”
> 
> > b  
> > =\> “HELLO”
