ruby rails binding.pry how to step

Billy picture Billy · Feb 23, 2016 · Viewed 7.3k times · Source

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

Answer

David Stump picture David Stump · Feb 23, 2016

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.