Ruby Newbee wrote: > > irb(main):039:0> hash = Hash.new [] > => {} > irb(main):040:0> hash['abc'] << 3 > => [3] > irb(main):041:0> hash.keys > => [] > irb(main):042:0> hash['def'] > => [3] > > > Since I have created a key/value pair with hash['abc'] << 3, why > hash.keys show nothing? > And, why hash['abc'] << 3 changed the hash's default value (which > should be an empty array)? I'm not sure why hash.keys was empty. But with regard to the default value, we are providing an object (a specific instance of the Array class) to be used as the default. We can illustrate this by asking Ruby to show us the object_id of the default object: irb(main):001:0> ary = [] => [] irb(main):002:0> ary.object_id => 20919800 irb(main):003:0> h = Hash.new ary => {} irb(main):004:0> h['foo'].object_id => 20919800 irb(main):005:0> h['bar'].object_id => 20919800 So we can see the object_id of the default object is indeed the same object_id of the array we provided to Hash.new. * * * We can instead provide a block to Hash.new which allows us to create a _new_ unique object (instance of Array) whenever a new default value is needed: irb(main):001:0> h = Hash.new {|h,k| h[k] = []} => {} irb(main):002:0> h['abc'] << 3 => [3] irb(main):003:0> h.keys => ["abc"] irb(main):004:0> h['def'] => [] Regards, Bill