From: Josh Cheek Date: 2009-12-29T15:20:54+09:00 Subject: Re: PLEASE HELP...dup is not working correctly in the following code --000e0cd1b124074e53047bd806c3 Content-Type: text/plain; charset=ISO-8859-1 On Mon, Dec 28, 2009 at 11:40 PM, timr wrote: > #dup creates a copy of an object with a different object_id. As > follows: > >> a = [1,2,3] > => [1, 2, 3] > >> b = a.dup > => [1, 2, 3] > >> b << 1000 > => [1, 2, 3, 1000] > >> a > => [1, 2, 3] > >> b > => [1, 2, 3, 1000] > > This is the difference $ irb >> a = [ 'w' , 'x' , 'y' ] => ["w", "x", "y"] >> b = a.dup => ["w", "x", "y"] >> a << 'z' => ["w", "x", "y", "z"] >> b => ["w", "x", "y"] >> a[0] << 'x' => "wx" >> a => ["wx", "x", "y", "z"] >> b => ["wx", "x", "y"] a and b are different arrays, but they each contain the same objects. I think the only way to make a deep copy is to explicitly dup everything in the array also, or to use marshall. I'm not sure why there isn't a deep copy method. --000e0cd1b124074e53047bd806c3--