From: John Carter Date: 2003-12-03T09:53:05+09:00 Subject: Re: Rite, Block locals and Autovivification Autovivification in Perl is simplified by the fact that in the case of "$a{$b}" Perl knows from the '{' that a hash is involved. In Ruby, it is not so easy, but clearly "a[b]" doesn't make sense if a.kind_of? Array and !b.kind_of?( FixNum), so "Do What I Mean" says you should autovivify a Hash if appropriate. Likewise whether in Perl "$a{$b}++" or ruby "a[b]+=1" the element at a[b] should be autovivified as 0. In the case of a[b]*=c the only thing that makes sense is if a[b] autovivified as 1 On Wed, 3 Dec 2003, Bill Kelly wrote: > Hi, > > From: "John Carter" > > > > def histogram( enum) > > # <--- Implicit hash = Hash.new(0) here > > enum.each{|e| hash[e]+=1} # Hash autovivifies. > > eh? But... somevar[e] ... Why would Ruby assume I wanted a Hash > object? That could be an Array, or a String, or....... > > Do you mean the autovivification would happen only if the variable > was actually _named_ 'hash' ? > > Or am I not understanding your example? > > > # Since hash autovivified we expect it to be available > > # outside the block scope. > > hash.keys.sort{|a,b| hash[a]<=>hash[b]}.each do |k| > > puts "#{k}\t#{hash[k]}" > > end > > end > > > Regards, > > Bill > > > John Carter Phone : (64)(3) 358 6639 Tait Electronics Fax : (64)(3) 359 4632 PO Box 1645 Christchurch Email : john.carter@tait.co.nz New Zealand A Million Monkeys can inflict worse things than just Shakespeare on your system.