From: Robert Klemme Date: 2009-06-20T01:00:30+09:00 Subject: Re: hash of hash? On 19.06.2009 17:42, Jeroen Lodewijks wrote: > Thanks all for the replies, very clear. > > I think I will implement an accessor to the hash, I come from a Perl > world and would build the hash in a seperate routine (to hide the > initialisation) that returns a reference to the hash. If you create a new class then the constructor is just sufficient - you do not need another method that hides away invocation of YourClass.new. > Would it be possible to make Hashhash inherit from Hash (by using < > Hash) and be able to use the [][] operator immediately in a way? Actually, you do not need to create another class (see my earlier example). For that it might make sense to create a method that returns a new instance with the initialization I have shown. If you lack explanation: the construct Hash.new {|h,k| ...} creates a new Hash instance with its default_proc set to the block you provide. This block is invoked whenever there is a key miss. In my case I just built it in a way as to create another Hash on the fly with the same default_proc and stuff it in the other Hash. The default_proc propagation ensures you can arbitrarily nest Hashes. Of course, you can also do something like this if you know you just need one level of nested Hashes ha = Hash.new {|h,k| h[k] = {}} Question is whether you want to bother to create a class for this. IMHO this makes only sense if you want to put additional logic into it (e.g. checking elements that are placed in the Hash). Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/