Reputation: 696
Why does the value in my data_dummy hash increase? I’d like to use it to initiate another hash with zero values!
fau[f.label][:hash] = data_dummy # ==>{"name 1" => 0, "name 2" => 0} but in the second loop it contains data from the first loop e.g. {"name 1" => 2, "name 2" => 0}
When using the string instead of variable dummy_data the code works as expected.
fau[f.label][:hash] = {"name 1" => 0, "name 2" => 0}
I can't do that because 'name X' is changing....
That's strange to me!
complete code
fau = {}
series = []
labels = [{:value => 0, :text => ''}]
data_dummy = {}
source.each do |c|
data_dummy[c.name] = 0
end
i = 0
data_dummy.each do |k,v|
i += 1
labels.push({:value => i, :text => k})
end
source.each do |s|
logger.debug "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
logger.debug "Source: '#{s.name}'|'#{fault_labels[s.fault_id.to_s].label}' => #{s.cnt}"
con_name = s.name #TODO: Cut name (remove location like left,right, ...)
f = fault_labels[s.fault_id.to_s]
unless fau.has_key?(f.label)
# init faults-hash 'fau'
fau[f.label] = {:total => 0, :hash => {}, :color => f.color, :name => f.label} #, :data => []
# add all connector_names as keys with value = 0
logger.debug "init :hash with #{data_dummy}" # ==>{"name 1" => 0, "name 2" => 0} but in the second loop it contains data from the first loop e.g. {"name 1" => 2, "name 2" => 0}
fau[f.label][:hash] = data_dummy
# this way the number of incidents are all in the same order for each fault (first dimension key)
# and all get at least a value of 0
end
logger.debug "Count up fau['#{f.label}'][:total] = #{fau[f.label][:total]} + #{s.cnt} (where connector '#{s.name}' and fault '#{f.label}')"
logger.debug "Count up fau['#{f.label}'][:hash]['#{con_name}'] = #{fau[f.label][:hash][con_name]} + #{s.cnt}"
fau[f.label][:total] += s.cnt
fau[f.label][:hash][con_name] += s.cnt
logger.debug "result :hash with #{fau[f.label][:hash].inspect}}"
end
Upvotes: 2
Views: 242
Reputation: 4800
Because Ruby hashes, like all Ruby objects, are references and copying one, such as hash2 = hash1
only creates a copy of the reference. Modifying hash2 will modify hash1, as really, they are just different aliases for the same thing.
You want to use the clone method instead.
hash2 = hash1.clone
See also How do I copy a hash in Ruby?
Note that even this only creates a shallow copy, if you have a nested hash (such as myhash = {"key1" => "value1", "key2" => {"key2a" => "value2a"}}
), you will have to make a deep copy. According to Wayne Conrad's answer to the question above, the way to do that is this:
def deep_copy(o)
Marshal.load(Marshal.dump(o))
end
Upvotes: 5
Reputation: 45087
If you want to make a copy of the hash, you need to use the dup
method:
foo = {"name 1" => 0, "name 2" => 0}
bar = foo
foo["name 2"] += 1
foo
=> {"name 2"=>1, "name 1"=>0}
bar
=> {"name 2"=>1, "name 1"=>0}
baz = foo.dup
foo["name 2"] += 1
foo
=> {"name 2"=>2, "name 1"=>0}
baz
=> {"name 2"=>1, "name 1"=>0}
Upvotes: 2