From: Gavin Kistner Date: 2005-10-06T23:01:23+09:00 Subject: Re: Can Ruby pop like Lisp? --Apple-Mail-1--23791011 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed On Oct 5, 2005, at 10:17 PM, Kevin Brown wrote: > On Wednesday 05 October 2005 21:57, Gavin Kistner wrote: >> On Oct 5, 2005, at 9:07 PM, Kevin Brown wrote: >>> A different array that contains the same objects is a deep copy. A >>> reference >>> to the same array is called a shallow copy, because the two >>> 'pointers' point >>> to the same physical memory. (jee, can you tell I came to Ruby from >>> C++?) >> >> I would disagree. The terminology I'm used to using and hearing >> states it as David did: >> >> A shallow copy is a new object of the same type holding references to >> the same objects as the original. >> >> A deep copy is a new object of the same type, where each 'child' >> object in the original is (recursively) deep copied. > > Which is what I just said minus the recursively. I apologize for > missing that > crucial piece. What was originally stated by David was that a > different > array containing the same objects was a shallow copy. It is not > fully deep, > nor is it fully shallow. That's all. I think you and I are still saying different things. Here's what I (and I believe David) am saying. a,b = 'a', 'b' c = [ a, b ] same = c shallow = c.dup deep = [ a.dup, b.dup ] 'same' is a reference to the same array. It is not, in any way, a copy. This seems to be what you called a 'shallow copy'. 'shallow' is what I would call a shallow copy - you can push a new element onto the array without affecting the original, but if you mutate the existing elements in the array, you affect the elements in the original. (For example, shallow[0]<<'HEY' will affect c[0].) This seems to be what you called a 'deep' copy. 'deep' is what I would call a deep copy. (Although normally I would use a more automated technique to create it :) --Apple-Mail-1--23791011--