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
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'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.
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.
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.
next
executes that line of code and proceeds to the next line. step
steps into a function. quit
lets the program continue running.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With