From: Edmond Kachale Date: 2011-10-25T22:50:21+09:00 Subject: Unusual behavior of a Ruby hash --0016362845e403323704b01fd204 Content-Type: text/plain; charset=ISO-8859-1 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* --0016362845e403323704b01fd204--