Rubysters,
I had the following code working nicely:
(a) http://pastie.org/private/8qn4f36mfeppbobm7b2wsa
At the beginning of every iteration, I expect “essential_params” hash
(in
line 8) to look like this: essential_params = {:val_1 => 0, :val_2 =>
[x,y,z], :val_3 =>0}. However it does repetitive work to initialize a
hash
from a database (in line 8).
So as to reduce repetitive database querying (at every iteration), I
have
just moved hash initialization to (line 7) outside a loop:
(b) http://pastie.org/private/iudfe0q2s41vszxby6vva
Now the code acts wierd: At every iteration, my “essential_params” hash
contains data accumulated from the previous iterations. For example, at
the
nth iteration, essential_params may look like this: {:val_1 => 25,
:val_2 =>
[x,y,z], :val_3 =>24} which are results from (n-1)th iteration (instead
of
the default values: {:val_1 => 0, :val_2 => [x,y,z], :val_3 =>0}).
Does anyone have a better explanation about this? Are Ruby hashed
assigned
by value or by reference? (Specifically, why is the pre-populated hash
at
line 7 in (b) getting values from lines 22 and 23 ? )
Kind regards,
–
Edmond