Basic PRY Commands For Beginners

José Romero
Nov 7 · 3 min read

During a lecture on Lab Review: Ruby Boating School I asked a question about what other commands we could use on binding.pry and the coach said “I don’t know, that would be a good blog idea.”

After patting myself on the back for asking such a good question, here we are. I’ve done some research and found some other PRY commands that I believe would be helpful for any beginner. Let’s get started.

As an example we are going to use the class Driver seen in the next picture.

Moving forward I’m assuming that you already know how to install and get inside the binding.pry. If that is the case you should be seeing something similar to the next picture. However if you do not already now how to do that you should stop here and follow this link. There you can read more about PRY and how to install it to start debugging your code.

As you can see my cursor is on the class main.

Using the command cd takes you inside the class Driver.

Cd moves into the new object or scope inside a PRY session.

Next, type cd Driver and PRY shows us that we are now located inside our class Driver.

The command cd .. takes us back to the previous scope. In this case that is the class Main as you can see in the line 3 below.

Now to go back to the class Driver type cd Driver

Bonus for cd:

cd and cd / will both take you to the top level.

The command ls by default shows you the local variables, public methods, instance variables declared on the current object.

Here some more examples of ls you can try on your own:

>> ls -m (show public methods defined on the object)

>> ls -i (show instance variables and class variables)

>> ls -h (shows help, all the usage options)

As you can see in the next picture:

In line 1 cd is used to get into the driver object, which is carlos. Then because carlos is an instance of the class Driver that places you inside the class Driver.

As you can see in the line 2 ls was used and shows the instances methods, class methods, instance variables, class variables.

The nesting command displays the nesting level. (See line 4)

[1] pry(main)> cd carlos
[2] pry(#<Driver>):1> ls
Driver#methods: distance_driven name passengers rides
self.methods: __pry__
instance variables: @name
class variables: @@all
locals: _ __ _dir_ _ex_ _file_ _in_ _out_ _pry_
[3] pry(#<Driver>):1> cd name
[4] pry("Carlos"):2> nesting
Nesting status:
--
0. main (Pry top level)
1. #<Driver>
2. "Carlos"
[5] pry("Carlos"):2>

Now that you have some basic PRY command knowledge get ready to start debugging like a pro.

Sources:

>> https://github.com/pry/pry

José Romero

Written by

Paraguayan in America : coding around the world.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade