Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I watch a variable in Ruby Pry?

I'm using Ruby 2.2.2, Pry and 'pry-byebug'. The continue statement removes any watched variables in have in pry-byebug:

[1] pry(main)> watch foo
Watching foo
watch: foo => 42
[2] pry(main)> watch
Listing all watched expressions:

1: foo => 42

[3] pry(main)> continue
[1] pry(main)> watch
No watched expressions

Losing them on every continue makes watched expressions pretty worthless. If I use next and step to the same point in the code instead, the watched expressions are still there; it's just the continue that causes the problem. At the moment I can't even find any documentation on the watch statement, so I don't know why this occurs.

More generally, I just want to print out the value of a set of expressions on every Pry prompt (or, perhaps, print them out if they've changed since the last time they were printed.) How can I achieve this in a Ruby debugger?

like image 392
Sasgorilla Avatar asked Jan 23 '17 16:01

Sasgorilla


People also ask

How do you use pry in Ruby?

The Ruby programmer can invoke the pry console during runtime by inserting the line 'binding. pry' wherever they would like to stop the program. When the interpreter hits the binding. pry, Pry will open a REPL session in the console, allowing you to test variables, return values, iterations, and more.

What are watch variables?

Any item of data within a program that a programmer wants to observe when debugging. Watch variables may be viewed while the program runs on its own, is stepped through instruction by instruction or when the program crashes. Setting watch variables is part of the debugging operation in a compiler.

How do I use remote pry?

remote_pry . When the breakpoint is hit, pry-remote will block your app and open up a DRb endpoint that a client can connect to. Running pry-remote in a shell will then connect to the session and you'll be able to interact with Pry as you normally would. Simply call exit in the Pry session to unblock the application.

How do I use Byebug in Ruby?

wherever you'd like the application to "break" - that is, executing byebug is equivalent to putting a breakpoint in your code. Run the program and use the debugger commands once you reach the breakpoint. near the end. Restart your server.


1 Answers

Using pry-moves you can use watch variable - this will display value of variable on each step.

like image 126
Daniel Garmoshka Avatar answered Oct 04 '22 15:10

Daniel Garmoshka