On testing with ruby debugger block_given? gives false but still executes, can someone explain me how its executed?.It is anything related to context(Is debugger changing context)?
if yes then how to find current context.
Now instead of ruby-debug when used pry then block_given? returns true.
def test
debugger
if block_given?
yield(4)
else
puts "ss"
end
end
test {|el| puts "#{el}" }
This looks like a bug in ruby-debug, specifically Kernel#binding_n which is what is getting used by the debugger.
You don’t need to go into the debugger read/eval/print loop to see the bug. Here is a non-interactive example example replacing the debugger() call with binding_n(0):
Pry doesn’t have this problem probably because it uses Ruby’s binding() rather than binding_n(). Of course that is the most reliable.
In the case above this is clearly a win here since binding() is what is exactly wanted. But as awesome as Pry is, it can’t evaluate expressions in any stack frame context except the current (or most recent) stack frame.
See also http://banisterfiend.wordpress.com/2011/01/27/turning-irb-on-its-head-with-pry/#comment-274 for differences between Pry and a debugger such as ruby-debug.
Lastly, I’ll note that I tried this both in the patched MRI 1.9.2 trepanning debugger as well as rbx-trepanning for Rubinius 1.2-ish.
Both of them work as expected. In both, the equivalent of binding_n is done differently with more help from the Ruby runtime. That leaves rb8-trepanning which has the bug because it uses binding_n as well.