From: Robert Klemme Date: 2004-06-25T16:38:05+09:00 Subject: Re: N Dimensional Hash? "Shashank Date" schrieb im Newsbeitrag news:2k0g6fF15rigjU1@uni-berlin.de... > > "Robert Klemme" wrote in message > > > Alternatively you can use Arrays as hash keys. > > I always thought that Arrays cannot be used as hash keys, > but after reading your email and re-reading Pickaxe (pg. 207) > I learnt that you _can_ use them albeit with a little > inconvenience: that you have to call Hash#rehash method > to reindex the hash whenever a key hash changes. You should consider hash keys as constants. You can even enforce that by inserting a frozen copy but that's not needed. Just don't reuse the instance after you put it into the hash: # arrays as keys hash={} [ %w{mini cooper red}, %w{ford focus white}, %w{ford mustang blue}, %w{ab cd 1}, %w{abc d 2}, %w{a bcd 3}, ].each do |a| hash[a] = a.pop end p hash p hash[%w{ford focus}] # hash of hashes i=lambda {|h,k| h[k] = Hash.new &i} hash = Hash.new &i [ %w{mini cooper red}, %w{ford focus white}, %w{ford mustang blue}, %w{ab cd 1}, %w{abc d 2}, %w{a bcd 3}, ].each do |a| val = a.pop key = a.pop a.inject(hash) {|h,k| h[k]}[key] = val end p hash p hash["ford"]["focus"] Kind regards robert