Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call a method and immediately step into it

Tags:

ruby

pry

When, during a pry debug session, I want to inspect step-by-step execution of FooClass.new.foo I'd do this in pry console

$ FooClass.new.foo #this gives me path and line of the method
break /path/to/foo_class.rb:LINE_WHERE_FOO_IS_DEFINED
FooClass.new.foo

This works but I need to look for the path, line and it leaves a breakpoint I sometimes have to remove.

There's a faster way:

break FooClass#foo
FooClass.new.foo

but it's still two steps, and the breakpoint stays.

Is there a way to do it in one command, like

step-into FooClass.new.foo

which would start a Pry subsession, enter the method execution and after it exits/finishes I'm back in the original session with no extra breakpoints left?

In other words: I'm in the middle of debugging, and see a method called few lines before (I can't step into it immediately). I don't want to put a binding.pry in the source (it may take a lot of time to start the debugging session again).

like image 535
Grzegorz Avatar asked May 01 '19 09:05

Grzegorz


People also ask

What is step into step over and step out?

step into -> go into the subroutine and wait for next action. step over -> jump over the subroutine without waiting again. step out -> if you are in the subroutine, you will leave it without waiting again.

How do I step through code in Intellij?

Force step over From the main menu, select Run | Force Step Over or press Alt+Shift+F8 .

What happens when we use step into while tracing?

Step into – An action to take in the debugger. If the line does not contain a function it behaves the same as “step over” but if it does the debugger will enter the called function and continue line-by-line debugging there.

How do you step over in Visual Studio?

When you are on a line of code that is a function or method call, you can press F10 (Debug > Step Over) instead of F11. F10 advances the debugger without stepping into functions or methods in your app code (the code still executes).


1 Answers

The problem has hidden complexity. For instance, one thing you can do is:

pry Foo.new.foo
or 
Foo.new.foo.pry

This puts you into a binding on the return value of the function foo, but that is not what you want. It also shows there is complexity when your line of code Foo.new.foo runs, for a function to know what exactly to put a binding on.

So that's really the problem I feel; it would take understanding of the expression on the line to understand where to put a breakpoint. If you imagine it was working like the step function.

If your code Foo.new.foo was already a break point (e.g. you had a function with that code in and you put a breakpoint on a line like that) then you typed step, you would go into Foo.new Foo.initialize instance of foo foo.foo and so on. So, any code trying to do what you would like it to do would have a very hard time understanding where in the expression tree to stop and put your into pry.

This is why you have to tell pry where to stop using break in this context.

like image 109
Sam Winstanley Avatar answered Oct 04 '22 16:10

Sam Winstanley