On 3 Sep 2007, at 15:31, 7stud -- wrote:

> Robert Klemme wrote:
>> $ 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.
>>
>
> I don't know what that output means.

Each line shows an 'event' occurring in the script. Where event is  
calling a C method, returning, etc... (see Kernel#set_trace_func). We  
can summarise what's going on:

>> ["line", "-e", 1, nil, #<Binding:0x1002fdf4>, false]

Process new line.

>> ["c-call", "-e", 1, :[], #<Binding:0x1002fdb8>, Hash]

Call the C method Hash#[]

>> ["c-call", "-e", 1, :default, #<Binding:0x1002fca0>, Hash]

Call the C method Hash#default.

>> ["c-call", "-e", 1, :call, #<Binding:0x1002fb9c>, Proc]

Call the C method Proc#call. This Proc is the one given to Hash.new I  
guess. Here is the answer to your original question.

>> ["line", "-e", 1, nil, #<Binding:0x1002f9a8>, false]

I guess this means we've moved into the Proc

>> ["c-call", "-e", 1, :[]=, #<Binding:0x1002f8a4>, Hash]

We call Hash#[] inside our Proc

>> ["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]

Return from each of the previous calls.

>> ["c-call", "-e", 1, :<<, #<Binding:0x1002f4a8>, Array]
>> ["c-return", "-e", 1, :<<, #<Binding:0x1002f3b8>, Array]

Push something onto the Array.

So short answer: Hash#[] calls Hash#default which in the case of Hash  
objects with an associated Proc calls the Proc.

Alex Gutteridge

Bioinformatics Center
Kyoto University