A
Alex Stahl
Hi Folks - Using Ruby 1.8.7, I found this oddity:
require 'rubygems'
require 'json'
setup = []
ids = ["12345", "54321", "21345", "548752", "3215", "50203"]
doIt = {
"action" => "do something",
"result" => "true"
}
cnt = rand(10)
1.upto(cnt) do
doIt['params'] = ids[rand(6)]
p "Pushing step: #{doIt.inspect}"
setup << doIt
end
p "Final steps list: #{setup.inspect}"
**************************************
When I inspect my final steps list at the end, the values of
'doIt['params']' is always the same - set to the last value of the last
run of the upto loop. But when I inspect doIt prior to pushing it, I
can see that it has its own value, acquired randomly.
So my question is, why is the hash not being pushed onto the array
properly? What side effect am I encountering here and how do I get
around it?
(I've already tried declaring "params" => "" in the initial hash
creation; that didn't help).
Thanks,
Alex
PS - You'd be correct in noting this code doesn't do anything useful.
I've stripped away other parts to isolate the questionable behavior for
demonstration purposes.
require 'rubygems'
require 'json'
setup = []
ids = ["12345", "54321", "21345", "548752", "3215", "50203"]
doIt = {
"action" => "do something",
"result" => "true"
}
cnt = rand(10)
1.upto(cnt) do
doIt['params'] = ids[rand(6)]
p "Pushing step: #{doIt.inspect}"
setup << doIt
end
p "Final steps list: #{setup.inspect}"
**************************************
When I inspect my final steps list at the end, the values of
'doIt['params']' is always the same - set to the last value of the last
run of the upto loop. But when I inspect doIt prior to pushing it, I
can see that it has its own value, acquired randomly.
So my question is, why is the hash not being pushed onto the array
properly? What side effect am I encountering here and how do I get
around it?
(I've already tried declaring "params" => "" in the initial hash
creation; that didn't help).
Thanks,
Alex
PS - You'd be correct in noting this code doesn't do anything useful.
I've stripped away other parts to isolate the questionable behavior for
demonstration purposes.