From: Edmond Kachale Date: 2011-10-28T15:00:19+09:00 Subject: Re: Unusual behavior of a Ruby hash --0016e64ed51479bf0c04b0559a3c Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable 2011/10/27 Jes=FAs Gabriel y Gal=E1n > If you need to deep copy a nested object structure you can use this idiom= : > > x =3D [[...]] # whatever, a nested set of arrays, etc > y =3D Marshal.load(Marshal.dump(x)) > That was a program saver!! It has worked the "magickie" like Superman!! (Indeed, One key to career growth is never to stop learning ( http://tek.io/n4EDNP)) --- *Edmond* * One key to career growth is never to stop learning (http://tek.io/n4EDNP)* 2011/10/27 Jes=FAs Gabriel y Gal=E1n > 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=FAs Gabriel y Gal=E1n > > > >> Well, first of all, it seems that essential_params has a key > :params_hash, > >> so I guess it's something like: > >> > > > >> essential_params =3D {:params_hash =3D> {:val_1 =3D> 0, :val_2 =3D> [x= ,y,z], > >> > > :val_3 =3D>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] =3D 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 u= p. > 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 tha= n > 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 =3D [[...]] # whatever, a nested set of arrays, etc > y =3D 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. > > --0016e64ed51479bf0c04b0559a3c--