From: "Aaron D. Gifford" Date: 2011-12-20T05:10:05+09:00 Subject: Re: Understanding array.clear On Mon, Dec 19, 2011 at 12:48 PM, Nick Sandberg wrote: > I was working a project Euler problem and noticed that when I used this > code: > > array1 = ["a", "b", "c", "d", "e"] > array2 = Array.new > array2 << array1 You just stored the object array1 inside of array 2. > array1.clear Having cleared array1 (emptied it out) by calling the Array instance in array1's clear method... > p array2 > > [[]] is my output. But, when I tried: ...you see the expected output, since the array1 object is now empty. > > array1 = ["a", "b", "c", "d", "e"] > array2 = Array.new > array2 << array1 > array1 = [] Here you are NOT clearing out array1, but assigning a brand new array object to the variable array1. So the old array1 is still in existence (and still stored in array2). > p array2 > > [["a", "b", "c", "d", "e"]] is my output.  I am not understanding why > clearing array1 is changing array2, but replacing the value of array1 is > having no effect. I hope this clears it up a bit. <> Aaron out.