Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ruby rails binding.pry how to step

What are the terminal commands to step over a line of code while using ruby rails 'binding.pry'? In addition do you know the command to step into, step out of, and continue?

Here is an example:

def add_nums
    x = 5 + 5
    binding.pry
    x += 5
    x += 7
    return x
end  

I'd like to know how to step through this method and in my terminal to see what the value of 'x' is until it is returned. Thanks

like image 528
Billy Avatar asked Feb 23 '16 02:02

Billy


People also ask

How do you get to the next step in binding pry?

If you're in regular old Pry you can use exit to go to the next binding. pry or disable-pry to exit Pry entirely.

What is binding pry in Ruby?

What's 'binding'? Binding is a built-in ruby class whose objects can encapsulate the context of your current scope (variables, methods etc.), and retain them for use outside of that context. Calling binding. pry is essentially 'prying' into the current binding or context of the code, from outside your file.

How do I debug with pry?

Invoking pry debugging To invoke the debugger, place binding. pry somewhere in your code. When the Ruby interpreter hits that code, execution stops, and you can type in commands to debug the state of the program.


2 Answers

Inelegant Solution

Since you have access to the scope of x, manually enter each line (or anything you want) and see how it impacts your variable.

More Elegant Solution

Check out either PryDebugger (MRI 1.9.2+) or Pry ByeBug (MRI 2+) which give you controls to manually step through code. If you choose ByeBug the brief syntax example is:

def some_method
  puts 'Hello World' # Run 'step' in the console to move here
end

binding.pry
some_method          # Execution will stop here.
puts 'Goodbye World' # Run 'next' in the console to move here.

Hope this helps.

like image 73
David Stump Avatar answered Oct 18 '22 06:10

David Stump


next executes that line of code and proceeds to the next line. step steps into a function. quit lets the program continue running.

like image 4
gkee Avatar answered Oct 18 '22 05:10

gkee