From: Martin DeMello Date: 2008-10-09T02:09:13+09:00 Subject: [ruby-core:19211] Re: [Bug #622] merging nested hashes On Wed, Oct 8, 2008 at 4:13 AM, izzet emre kutlu wrote: > > Shouldn't merge method return a new hash? > Why is new hash referencing back to default[:opts] hash in this code? > > Is it a bug or is there something I am missing? merge doesn't do a deep copy irb> class A; attr_accessor :a; end => nil irb> foo = A.new => # irb> foo.a = 10 => 10 irb> hash1 = {"foo" => foo, "bar" => "bar"} => {"foo"=>#, "bar"=>"bar"} irb> hash2 = {"hello" => "world"} => {"hello"=>"world"} irb> hash3 = hash1.merge(hash2) => {"foo"=>#, "hello"=>"world", "bar"=>"bar"} irb> hash1 => {"foo"=>#, "bar"=>"bar"} irb> hash2 => {"hello"=>"world"} irb> hash3 => {"foo"=>#, "hello"=>"world", "bar"=>"bar"} irb> foo.a = 42 => 42 irb> hash1 => {"foo"=>#, "bar"=>"bar"} irb> hash2 => {"hello"=>"world"} irb> hash3 => {"foo"=>#, "hello"=>"world", "bar"=>"bar"} martin