Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8593783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:04:06+00:00 2026-06-12T00:04:06+00:00

Why does the value in my data_dummy hash increase? I’d like to use it

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T00:04:08+00:00Added an answer on June 12, 2026 at 12:04 am

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a universal data type, which is passed by value, but does not
Why when a control does contain value but it's set to .Enable=False that all
Does the multipart/form-data value of the enctype <form> attribute has any use except for
I have the following LINQ Select which does not work. Data.Select(d => d.Value.IsDirty =
Does std::map move around already inserted values when inserting new data ?
What value does the hashCode() method return in java? I read that it is
What is a scalable algorithm to print an N-binary-digit integer manually whose value does
Does setting this value have the same effect as setting the debug=true in the
Does the timezone value change throughout the year to adjust to DST? So if
I have the code below. I'm wondering that does the self.value and _value have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.