Alle 18:37, giovedì 7 dicembre 2006, WKC CCC ha scritto: > unknown wrote: > > WKC CCC <wai-kee.chung / uk.bnpparibas.com> wrote: > >> count = count + 1 > >> end > >> > >> puts one.inspect > > > > Array.new(array) copies the *array* but it does not copy its *elements*. > > So tempArr[0] is another name for the very same object as one[0], and so > > forth. m. > > If they are referring to the same object, why is it when > > tempArr = Array.new(one) > one.clear > > results in tempArr still having the values originally assigned to array > one? > > Thanks, tempArray and one are two different objects (as you can see using object_id), but the objects they contain are the same (again, use object_id to check this: one[0].object_id and tempArray[0].object_id return the same value). When you call one.clear, you are changing the array itself, not the objects it contains. Instead, when in your block you call x.concat (where x is one of the elements of tempArray), you aren't changing the tempArray, but its elements. The array and the objects it contains don't speak to each other, so: * when you call an array's method (array.clear, array.reverse!,...), you change the array. The objects it contains don't change. * when you call an element's method (such as, in your example, concat), you don't change the array. Other variables referring to the same object, share that change. I fear my explanation makes this topic look more confusing than it actually is, but I hope it's useful. Stefano