Bill Kelly
12/2/2003 9:33:00 PM
Hi,
From: "John Carter" <john.carter@tait.co.nz>
>
> 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