Turning Pry into a debugger – pry-nav

Want to step through code or continue / next?

And add these to ~/.pryrc

https://github.com/nixme/pry-nav

Pry – Part 5 – Pry at runtime

Assuming you’ve halted the runtime at the point where you invoked the Pry REPL (see Pry – Part 4), you should have the Pry REPL.

A. The main ways of navigating the stack are the ls and cd commands.

ls shows you methods, constants and variables.

cd moves you into the new context (object or scope).

1. ls

2. cd

B. Apart from navigating state, there are two other commands that are very useful, show-method and show-doc.

1. show-method
This shows the source for a method or class.

2. show-doc shows the documentation by running the method comments through rdoc or yard

Pry – Part 4 – Debugger

You can inject a Pry REPL directly into somewhere that you want to debug.

Here’s an example of runtime invocation:

test.rb

when run using ruby test.rb:

So, code is shown around the REPL invocation line with a cursor showing where the runtime has stopped.

http://pryrepl.org

For the best source of Pry documentation: https://github.com/pry/pry/wiki

For more on Pry at runtime, see Pry – Part 5 – Pry at runtime.

Pry – Part 3

So, assuming you’ve been through Part 1 and Part 2, you should have the Hello World gem installed.

We can cd directly into the gem’s source code and look at it directly:

We can run commands directly by prefixing them with a period.
E.g. look at the file structure layout using tree:

Note that you may need to install tree on MacOSX. E.g.

You can cdd into other objects, e.g.

The prompt updates to show you what object you’re in.

And:

shows you the current nesting status, e.g.

Pop out using:

or jump to an index:

Input buffers:
Clear out:

Amend:
Say you want to amend a line:

Pry – Part 2

More on using Pry.

Let’s play around with it a little using the Hello World gem (if you haven’t already installed it, use: gem install hello_world). Then, in pry:

Some useful commands:

1. ls:
ls shows you which methods, constants and variables are accessible to Pry. By
default it shows you the local variables defined in the current shell, and any
public methods or instance variables defined on the current object.

So we can list, class methods:

i.e. nothing.

Instance methods:

i.e. this method is available.

And this is the default, i.e.

e.g.:

2. cd
cd moves you into a new context (object or scope). As in UNIX shells use cd .. to go
back, cd / to return to Pry top-level and cd – to toggle between last two
scopes.

So,

and now:

So, we can invoke it like this:

However, if we’re only interested in the output value and not the return value then we can use a semi-colon:

More in Pry – Part 3.

Ruby -rdebug: Print variable

Using ruby -rdebug, I put a breakpoint in at a line which used a bunch of other libraries.

But when I tried to output a variable using “p” the debugger said it was in a different file.

The consensus with -rdebug is that it’s pretty useless and unmaintained. Better is to use Pry.

Here’s the output I got:

debug.rb:130:in eval':undefined local variable or method query’ for #