Topic: How to duplicate a Hash?

Maybe a stupid question but can anyone tell me how to duplicate a hash?

I've tried this one:

a = { :bla => "bla" }
b = a.dup
some_method(a)

But after calling some_method "b" is modified the same way as "a".
What am I doing wrong?

Re: How to duplicate a Hash?

I'm not getting that, weird...

irb(main):001:0> hash_a = { :dog => 'sparky', :cat => 'kitty' }
=> {:dog=>"sparky", :cat=>"kitty"}
irb(main):003:0> hash_b = hash_a.dup
=> {:dog=>"sparky", :cat=>"kitty"}
irb(main):010:0> def some_method(hsh)
irb(main):011:1> hsh.merge!(:bird => 'keet')
irb(main):012:1> end
=> nil
irb(main):013:0> some_method(hash_a)
=> {:dog=>"sparky", :cat=>"kitty", :bird=>"keet"}
irb(main):014:0> hash_b
=> {:dog=>"sparky", :cat=>"kitty"}
irb(main):015:0>

vinnie - rails forum admin

Re: How to duplicate a Hash?

Actually in console it works for me, too. But not in my model code. Any idea what the reason could be?

some_method looks like this:

 def self.some_method(values)
   values.each { |time_range, a|
     a.map! { |project| project.tasks.sum(:amount) || 0 }
   }

   values.merge!(values) { |key, old_val, new_val| old_val.sum }
   values.merge({ '_title' => 'Some title' })
end