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:


when run using ruby test.rb:

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

For the best source of Pry documentation:

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

Code syntax highlighting when using WordPress and MarsEdit

How to add code syntax highlighting when using WordPress and MarsEdit

1. Install this:

2. Use Format > Customize to create a custom PRE tag class, e.g.

Other brushes:

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.


shows you the current nesting status, e.g.

Pop out using:

or jump to an index:

Input buffers:
Clear out:

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.


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


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


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

c. show-method
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:
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:

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

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 #