From: Robert Klemme Date: 2003-10-05T02:55:22+09:00 Subject: Re: hash of hashes "Joel VanderWerf" schrieb im Newsbeitrag news:3F7E3AF2.2050701@path.berkeley.edu... > Dave Brown wrote: > > In article <200310031434.31907.argentoff@rtelekom.ru>, > > Paul Argentoff wrote: > > : On Friday 03 October 2003 14:04, Paul Argentoff wrote: > > : > > : > How can I create $Subject for future population? > > : > > : Let's put it this way: I need to create a nested hash which will be populated > > : later by referencing the keys in the subhashes. How can I do this? > > > > You mean, er, > > > > irb(main):001:0> a=Hash.new({}) > > => {} > > irb(main):002:0> a['foo'] > > => {} > > > > ? > > > > --Dave > > That's like, a real bad trip, dude: > > irb(main):002:0> a=Hash.new({}) > => {} > irb(main):003:0> a[1] > => {} > irb(main):004:0> a[1]["x"] = "y" > => "y" > irb(main):005:0> a > => {} > irb(main):006:0> a[1] > => {"x"=>"y"} > irb(main):007:0> a[1] = 2 > => 2 > irb(main):008:0> a[2] > => {"x"=>"y"} > irb(main):009:0> a[1] > => 2 > > It would help to do > > a = Hash.new {{}} > > since you get a new hash each time... Not really: irb(main):001:0> h=Hash.new{{}} => {} irb(main):002:0> h[1][2]="foo" => "foo" irb(main):003:0> h => {} irb(main):004:0> You need a = Hash.new {|h,k| h[k]={}} as pointed out already. robert > >