From: Robert Klemme Date: 2007-09-03T14:55:04+09:00 Subject: Re: Hash.new with a block? 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, #, false] ["c-call", "-e", 1, :[], #, Hash] ["c-call", "-e", 1, :default, #, Hash] ["c-call", "-e", 1, :call, #, Proc] ["line", "-e", 1, nil, #, false] ["c-call", "-e", 1, :[]=, #, Hash] ["c-return", "-e", 1, :[]=, #, Hash] ["c-return", "-e", 1, :call, #, Proc] ["c-return", "-e", 1, :default, #, Hash] ["c-return", "-e", 1, :[], #, Hash] ["c-call", "-e", 1, :<<, #, Array] ["c-return", "-e", 1, :<<, #, Array] I believe there's also some explanation in the documentation. Kind regards robert