From: "Abinoam Jr." Date: 2011-05-04T11:15:16+09:00 Subject: Re: Iterating over an Array of Hashes Any problem with the marshal dump and load aproach of Chad Perrin? Look... arr = [{:fruit => {:kind => 'apple'}}, {:fruit => {:kind => 'banana'}}] Marshal.load(Marshal.dump arr).each do |h| h[:fruit][:kind] = 'coconut' end If it's ugly, you can try to beautify it... class Object def deep_copy Marshal.load(Marshal.dump self) end end arr.deep_copy.each do |h| h[:fruit][:kind] = 'coconut' end Abinoam Jr. On Tue, May 3, 2011 at 10:08 PM, John Feminella wrote: > Your example doesn't contain nested hashes, while mine does. That's > what I was demonstrating -- it's a shallow copy, not a deep one. > > ~ jf > -- > John Feminella > Principal Consultant, BitsBuilder > LI: http://www.linkedin.com/in/johnxf > SO: http://stackoverflow.com/users/75170/ > > > > On Tue, May 3, 2011 at 22:00, 7stud -- wrote: >> John Feminella wrote in post #996498: >>> On Tue, May 3, 2011 at 20:34, 7stud -- wrote: >>>> Peter Hicks wrote in post #996483: >>>>> >>>>> What is the magic I am missing? >>>>> >>>> >>>> clone(): >>> >>> `clone` doesn't cut it, since it's creating shallow copies. An >>> illustration of the problem: >>> >> >> Really?  Look at my output.  It cuts it just fine. >> >> -- >> Posted via http://www.ruby-forum.com/. >> >> > >