Incorrect line numbers in "caller" output when evaluated in a Proc binding
The second frame's line number appears to always be incorrect when a Proc is created via a non-toplevel method call.
This small program and its output illustrate the issue:
def first # line 1 of proc_caller.rb on my system
b = third do
1 + 1
eval("p caller(0)", first)
Output on recent 1.8 releases, including 1.8.6 and 1.8.7:
second'", "proc_caller.rb:6:in first'", "proc_caller.rb:16"]
Output that I would expect: (line 6 is in 'second', not 'first')
second'", "proc_caller.rb:2:in first'", "proc_caller.rb:16"]
Updated by marcandre (Marc-Andre Lafortune) about 13 years ago
- Category set to core
- ruby -v set to -
The expected output should be the same for
eval("p caller(0)", first.binding)
eval("p caller(0)", second.binding)
since the block has the same binding (i.e. that of second).
At the moment "p caller" is evaluated, both first and second have returned, so the output should not contain "proc_caller.rb:6". In neither case should we expect to see "proc_caller.rb:2:in `first'" in the backtrace.
It appears that when a block is captured (here by third), the binding is set the the full stack instead of just that of second. This leads to a longer backtrace, as seen with the output of caller or if an exception is raised in the eval.
Just to confirm that this bug is not present in Ruby 1.9; the output is quite sensible and the same from both first.binding and second.binding.