On 03.09.2007 06:50, 7stud – wrote:
In the code:
h = Hash.new {|hash, key| hash[key] = []}
what method is yielding the values hash, key to the code block? It
doesn’t seem like it could be new() since new() isn’t called when you
lookup a key in a hash. Does new() somehow associate that block with
the method [] in Hash?
$ ruby -e ‘h=Hash.new {|h,k| h[k]=[]}; set_trace_func(lambda {|*a| p
a}); h[1]<<2’
[“line”, “-e”, 1, nil, #Binding:0x1002fdf4, false]
[“c-call”, “-e”, 1, :[], #Binding:0x1002fdb8, Hash]
[“c-call”, “-e”, 1, :default, #Binding:0x1002fca0, Hash]
[“c-call”, “-e”, 1, :call, #Binding:0x1002fb9c, Proc]
[“line”, “-e”, 1, nil, #Binding:0x1002f9a8, false]
[“c-call”, “-e”, 1, :[]=, #Binding:0x1002f8a4, Hash]
[“c-return”, “-e”, 1, :[]=, #Binding:0x1002f868, Hash]
[“c-return”, “-e”, 1, :call, #Binding:0x1002f778, Proc]
[“c-return”, “-e”, 1, :default, #Binding:0x1002f688, Hash]
[“c-return”, “-e”, 1, :[], #Binding:0x1002f598, Hash]
[“c-call”, “-e”, 1, :<<, #Binding:0x1002f4a8, Array]
[“c-return”, “-e”, 1, :<<, #Binding:0x1002f3b8, Array]
I believe there’s also some explanation in the documentation.
Kind regards
robert