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

how do you change the syntax highlighting in Pry? Use Pry theme

Colours in Pry not quite how you want them?

Here’s how to change them:

To get rid of the warning message create a pryrc file and add a theme, e.g.

https://github.com/kyrylo/pry-theme

On the Mac, another alternative is to just change your Terminal preferences.

Questions on pry?

Hang out at #pry on irc.freenode.net

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.

Pry – an IRB alternative and runtime developer console – Part 1

A brief guide to Pry:

1. Install
gem install pry pry-doc –no-ri –no-rdoc

pry-doc lets you browse core C source
Other flags: don’t bother installing the docs ‘cos it takes extra time

2. Features
Run pry

Useful commands:
a. help help

Note: pry offers command line completion.

b. show-doc
Show the documentation for a method or class. Tries instance methods first and
then methods by default.

e.g. if we create an object like this:
s = “pry”
then we can do:
show-doc s.each_line

From: string.c (C Method):
Owner: String
Visibility: public
Signature: each_line(*arg1)
Number of lines: 30

etc…

Note, show-doc is smart. Can even read ri syntax.
E.g.
show-doc String#each_line
which gives the same documentation.

c. show-method
e.g.
show-method s.each_line

shows each line of the implementation for String.

-l gives you line numbers.

d. Share your code
gist s.each_line
creates a gist and copies the URL to your clipboard.

More info here: http://pryrepl.org
The screencast there is great although it’s for a slightly older version of pry.
E.g. autocomplete on gist-method doesn’t work because this method doesn’t exist any more. It’s just called gist now.

See also: http://railscasts.com/episodes/280-pry-with-rails

This post is getting rather long so the rest will be in Part 2.