On Thu, Oct 27, 2011 at 10:53 AM, Edmond Kachale
<kachaleedmond / gmail.com> wrote:
> In fact, Jesus should be the program saviour (i'm kidding).
>
> 2011/10/25 Jes=FAs Gabriel y Gal=E1n <jgabrielygalan / gmail.com>
>
>> Well, first of all, it seems that essential_params has a key :params_has=
h,
>> 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, =A0:params_hash in essential_params is an array of hash=
es.
>
> So, modify your line 15 to this:
>>
>> =A0 =A0 =A0new_data[:params_hash] =A0=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 up.=
 As
> it was rightly put by Jesus, I needed to duplicate
> essential_params[:params_hash]. Since essential_params[:params_hash] is a=
lso
> an array of hashes, at every iteration I have to duplicate each hash elem=
ent
> 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: *cl=
one
> * 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.