From: "Jesús Gabriel y Galán" Date: 2011-10-27T23:00:40+09:00 Subject: Re: Unusual behavior of a Ruby hash On Thu, Oct 27, 2011 at 10:53 AM, Edmond Kachale wrote: > In fact, Jesus should be the program saviour (i'm kidding). > > 2011/10/25 Jesús Gabriel y Galán > >> Well, first of all, it seems that essential_params has a key :params_hash, >> so I guess it's something like: >> > >> essential_params = {:params_hash => {:val_1 => 0, :val_2 => [x,y,z], >> > :val_3 =>0}} >> > > I agree. In fact,  :params_hash in essential_params is an array of hashes. > > So, modify your line 15 to this: >> >>      new_data[:params_hash]  = essential_params[:params_hash].dup >> >> and let us know if it worked for you. >> >> Hope this helps, >> > > I should say, it took me this long to get all this was getting messed up. As > it was rightly put by Jesus, I needed to duplicate > essential_params[:params_hash]. Since essential_params[:params_hash] is also > an array of hashes, at every iteration I have to duplicate each hash element > of the "essential_params[:params_hash]" array. Though I do not love the > repetitive "semi-manual" duplication, but it is much more efficient than the > repetitive database querying. > > In due course, I have learned two good methods and their differences: *clone > * and *dup* ( > http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/6326). > > Thanks folks!! If you need to deep copy a nested object structure you can use this idiom: x = [[...]] # whatever, a nested set of arrays, etc y = Marshal.load(Marshal.dump(x)) Take into account, though, that there are things that Marshal.dump will not dump, like default_procs of hashes, for example. But if you just have regular arrays and hashes it should be fine. Jesus.