# Assignment question

#Run this code

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

#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!

Kaye Ng wrote:

#Run this code

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

#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

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”