From: Rob Biedenharn Date: 2008-09-20T07:46:03+09:00 Subject: Re: overwriting hash key On Sep 19, 2008, at 6:37 PM, jim wrote: > How can get Ruby to tell me when I'm overwriting an existing key in my > hash? > > For example, > > h = { "key1" => [6, 44, 12], > "key2" => [8, 1], > . > . > } > > The hash could get big enough where I accidentally add a new entry, > > "key1" => [99, 3] > > when what I needed to do was add [99, 3] to key1's array. I won't get > any warning that the first occurrence of key1 is being replaced. Well, if you always have arrays for the values, you can start with: h = Hash.new {|h,k| h[k] = []} Then initialize your hash with: h.update({ "key1" => [6, 44, 12], "key2" => [8, 1], ... }) Then you can: h["key1"] << 99 << 3 h["key1"].concat([99, 3]) h["key1"].push(99, 3) h["key1"] has [6,44,12,99,3] -Rob Rob Biedenharn http://agileconsultingllc.com Rob@AgileConsultingLLC.com