Not sure why this isn't working

Chris,

In your remove_product method existing_product is a local variable, a
copy of an element in the @items array. When you manipulate
existing_product it has no effect on @items array. Here is a basic
example:

a = [1,2,3] => [1, 2, 3] b = a[1] => 2 b += 1 => 3 b => 3 a => [1, 2, 3]

Aaron

Hi --

Chris,

In your remove_product method existing_product is a local variable, a copy of an element in the @items array.

It's not a copy; it's a new reference to the same object.

When you manipulate existing_product it has no effect on @items array. Here is a basic example:

a = [1,2,3] => [1, 2, 3] b = a[1] => 2 b += 1 => 3

That's the same as:

   b = b + 1

You're re-initializing the local variable b; it no longer has any connection with what it was assigned to previously. So you're not really putting to the test the question of what happens when you manipulate the object you got from the array. (And you can't change integers anyway :slight_smile:

b => 3 a => [1, 2, 3]

Note the following, however:

   a = %w{a b c} => ["a", "b", "c"]    b = a[1] => "b"    b.upcase! => "B"    b => "B"    a => ["a", "B", "c"]

Here, b is a reference to the second element of a. I do an in-place upcasing of b, and since b and a[1] are the same object, a[1] is now uppercase.

David