Why Pry is one of the most important tools a junior Rubyist can learn

As programmers we often have to mentally run code. To imagine how a program will behave given certain inputs. This is hard enough for experienced developers. But for juniors? It can seem impossible. In this article, Melissa Williams argues that pry is an invaluable tool for junior rubyists because it allows them to see exactly what is going on as their code is run.

  • author photo By Melissa Williams
  • #ruby
  • Mar 3, 2020

Have you ever tried to debug your code by typing in puts statements and hoping they’d eventually show up where they should? You know what I mean - you type:

puts “This worked!"

into the code and then run it in the terminal, only to have nothing show up. Or maybe you’ve heard of the fancy debugging tools available to Rubyists but have been afraid to try them. So instead you go for the spaghetti method of debugging: throw code at your text editor and see what sticks. Problem is, this method doesn’t guarantee a solution.

Debugging is something you’re going to be doing for the rest of your coding life. It’s not just for junior devs! The sooner you learn the tools that help you find the bugs in your code, the better off you’ll be in the long run.

So what are the tools out there? While there are many choices, today we’re going to pry into one of the great tools every Rubyist should know how to use - Pry. We’ll learn how it works and why it’s important.

What is Pry?

Pry freezes your program so you can get inside your code and find out what’s going on and what needs fixing. Pry can look confusing at first, but take it from a fellow junior Ruby Dev - it gets easier the more you use it, and soon you’ll never want to program without it.

Pry is like IRB on steroids

Chances are, you’ve grown comfortable using IRB (Interactive Ruby Shell) where you can take your code and run it in a playground to find out what works and what doesn’t. It can also be a useful tool for debugging. Part of Pry works in a similar fashion, but with added features. Both IRB and Pry use REPL commands: Read, Evaluate, Print, and Loop. But Pry allows you to go further when debugging. For example, Pry gives you color-coded syntax, which helps when you’re trying to figure out what will happen when code is executed. It makes mistakes easier to spot, which makes your code easier to fix.

Sample code

Simple Pry Example

A note about debugging

It’s been said already, but it’s worth saying again - debugging is something that all programmers do throughout their careers. There’s nothing junior about learning these tools at all! So the sooner you learn them, the better a programmer you’ll be.

Setting breakpoints with Pry

Now it’s time to look at one of the very best features of Pry - setting breakpoints in your code. Let’s say you’re having a really hard time getting your code to run the way you expected. (We’ve all been there, right?)

What you can do is set a breakpoint in the part of the code that isn’t working as expected and then you’ll end up in a version of Pry, but at a point that is frozen. The best way to explain this is with an example.

Suppose we're trying to find out if the user we want to view is set properly. First, we can add our binding.pry into the code. Set the binding.pry right above the line of code that is causing the trouble and then run the code.

Setting our Breakpoint

Next, we try hitting the page that will trigger this code. Since we're running the rails server locally, we'll try going to: localhost:3000/users/1

You’ll find that you land in an IRB-like console where you can test out your code and see what’s happening.

Hitting Pry

At this point, we're able to check whether '@user' is set to the right user id.

Checking @user

We can also check whether our user is logged in:

Checking logged in status

What if nothing happens?

Sometimes you’ll place your breakpoints, run your code, and instead of landing inside Pry, nothing seems to happen. While this may be frustrating, it does give a clue. It means that your code never triggered the Pry because it never got that far. So what do you do? You set a new breakpoint earlier in the code and see if that works. While this may seem tedious, it is actually giving you important information!

When to ask for help

As a junior Rubyist, sometimes you'll need to ask a more senior developer for help. The problem is deciding when to keep debugging and when to call for help. When debugging, it's a good idea to try replicating the problem. Set your breakpoints with Pry and dig deeper into the code. If after 20-30 minutes you're no closer to finding a solution, it might be time to ask for assistance.

Give Pry a try

When you're ready to add Pry to your arsenal of debugging tools, you'll need to do a couple of things. First, you'll need to install the gem. You can do this by adding it to your gemfile and running a bundle install: gem 'pry'

Or, you can manually install it: gem install pry

You'll also need to make sure your code is expecting Pry, so place this line in your file:

require 'pry'

And that's it! You're up and running with Pry!

What to do next:
  1. Try Honeybadger for FREE
    Honeybadger helps you find and fix errors before your users can even report them. Get set up in minutes and check monitoring off your to-do list.
    Start free trial
    Easy 5-minute setup — No credit card required
  2. Get the Honeybadger newsletter
    Each month we share news, best practices, and stories from the DevOps & monitoring community—exclusively for developers like you.
    author photo

    Melissa Williams

    Melissa Williams is a technical content strategist and Ruby developer. She works with clients to help make technical content readable and accessible for all.

    Stop wasting time manually checking logs for errors!

    Try the only application health monitoring tool that allows you to track application errors, uptime, and cron jobs in one simple platform.

    • Know when critical errors occur, and which customers are affected.
    • Respond instantly when your systems go down.
    • Improve the health of your systems over time.
    • Fix problems before your customers can report them!

    As developers ourselves, we hated wasting time tracking down errors—so we built the system we always wanted.

    Honeybadger tracks everything you need and nothing you don't, creating one simple solution to keep your application running and error free so you can do what you do best—release new code. Try it free and see for yourself.

    Start free trial
    Simple 5-minute setup — No credit card required

    Learn more

    "We've looked at a lot of error management systems. Honeybadger is head and shoulders above the rest and somehow gets better with every new release."
    — Michael Smith, Cofounder & CTO of YvesBlue

    Honeybadger is trusted by top companies like:

    “Everyone is in love with Honeybadger ... the UI is spot on.”
    Molly Struve, Sr. Site Reliability Engineer, Netflix
    Start free trial