Debugging with Pry: A Beginner's Guide
Tags: ruby, pry, debugging
Published: 2019-02-25 04:10:55

---
title: Debugging with Pry: A Beginner's Guide
published: true
description: A brief introduction to Pry, a Ruby gem that makes debugging a breeze. 
tags: Ruby, Pry, debugging
---
---

While working through the pre-bootcamp coursework provided by Flatiron School, I constantly found myself struggling to check and debug return values within iterations and nested data structures. The Learn.co curriculum introduced a tool called Pry, but I had trouble understanding how to implement it in my code. It wasn't until a difficult lab by the name of Hashketball that things finally clicked. After struggling through the lab for far too long, I finally forced myself to read through Pry's documentation and my ability to write and debug hash iterations immediately improved. 

If  you ever find yourself struggling to build out a Ruby program, Pry could prove to be an invaluable tool in your arsenal. Pry is a powerful tool that Ruby developers can use to debug programs and push past hurdles. Like other Ruby gems, the Pry gem must be required in your environment settings or specific ruby file in order to access its functionality. Additionally, there are several Pry plugins, including pry-doc and pry-byebug, that expand the gem's features and capabilities. You can think of these plugins as separate gems that must be required in your gemfile.

Once you have configured your program with the Pry gem, it's time to pry open your code and experience the joy that is debugging with Pry. Pry is packed full of features but the following examples highlight what I believe to be the most useful. 

<h2>REPL (Read, Evaluate, Print, Loop)</h2>

Pry can replace Ruby's integrated IRB shell and builds on IRB's REPL functionality with some additional features. Both IRB and Pry allow you to execute Ruby commands in the terminal and receive immediate results in the terminal. Having REPL functionality in your terminal is a very useful tool that allows you to quickly test Ruby code without running your entire program. Let's say, for example, that you want to iterate over an array and see what values are returned. You can simply enter the array, use an iterator on that array, and check your results: 

<img src="https://thepracticaldev.s3.amazonaws.com/i/w7s09y2ig3zgo9axfhl6.png" height="350" width="350">

<img src="https://thepracticaldev.s3.amazonaws.com/i/lrycp6exdj0vs678yj4m.png" height="350" width="350">

Notice that while both IRB and Pry returned the same values for this simple iteration, the way Pry prints to the terminal looks a bit different. One of Pry's advantages over the standard IRB is its use of syntax highlighting. Other features that give Pry the edge over IRB include: 

1. Tab completion

2. Debugging tools

3. Ability to view documentation
 
<h2>Runtime invocation</h2>

Perhaps the most useful feature of Pry is its ability stop a program's runtime in its tracks. The Ruby programmer can invoke the pry console during runtime by inserting the line 'binding.pry' wherever they would like to stop the program. When the interpreter hits the binding.pry, Pry will open a REPL session in the console, allowing you to test variables, return values, iterations, and more.

The following is an example of the utility of Pry when it comes to dissecting nested data structures in your programs. I recently contributed to the development of a dev job search CLI that relied on a database of job openings seeded from the GitHub Jobs API. The API data was structured in a JSON hash and our database tables were seeded with values from these nested hashes. The following image shows how we populated the companies table for this project: 

<img src="https://thepracticaldev.s3.amazonaws.com/i/qrnbjk3xvtek6fbu8o5x.png" height="420" width="420">

When populating a database table, it's vital that records going into the table are created with the correct data! How might we check our iteration values to ensure that everything is working as expected? The answer is Pry. Inserting a binding.pry near the end of the "populating companies" block allows us investigate return values by stopping the runtime and entering a REPL session: 

<img src="https://thepracticaldev.s3.amazonaws.com/i/xmvlzg49wh6inokl97cv.png" height="420" width="420">

Once the binding.pry is inserted into the block, the code must be executed again in order to open the Pry console. When the Pry console opens in our terminal, we are within the block that is populating the companies database table and we are now able to test variables and return values: 

<img src="https://thepracticaldev.s3.amazonaws.com/i/lxkc6zjt4pycpbipgegv.png" height="420" width="420">

Now that we have pried open the "populate companies" block, it's time to check some values! When 'job' is inserted into the pry console, a hash for a single job opening is printed to the screen. The job hash has several keys including type, url, company, company_url, location, title, etc. To make sure the correct values are being sent to the database, we can enter the following commands into the Pry terminal: 

<img src="https://thepracticaldev.s3.amazonaws.com/i/7k3ypknzvzvoj86jmdgb.png" height="600" width="600">

Once we have determined that our iteration is functioning as expected, the binding.pry can be removed an our table will populate upon the next execution of the program!

<h2>Documentation and source code browsing</h2>

Let's face it, being a developer means you will be relying heavily on Google. I often turn to the web to research Ruby methods and scour documentation to ensure that I am correctly structuring syntax. What if I told you that Pry offers a plugin that allows you to browse documentation right in your console? Welcome to the pry-doc gem. Pry-doc enables you to read Ruby documentation within your current Pry console session. Now for an example. Let's say I can't remember how the .find method works on a hash. I can enter the following command into the console, and voilà! The Pry console prints the documentation for that method in the terminal: 

<img src="https://thepracticaldev.s3.amazonaws.com/i/3xmbk9dy6mfh1dubzob3.png" height="600" width="600">

Hooray! Now you can access useful documentation in your terminal without relying on Google! 


<h2>Useful Pry Commands</h2>

1. pry  -Opens the Pry console in your terminal

2. binding.pry  -Stops runtime and opens Pry console

3. exit  -Exits current loop

4. exit!  -Exits Pry console

5. whereami  -Prints current location within program

6. show-doc  -Displays documentation for a class or class method(s)

7. history  -Prints history of Pry commands to the terminal

8. ls  -Prints the variables and methods in the current scope of the program

9. help  -Prints the current list of available Pry commands

Pry drastically improved my ability to debug Ruby programs and is the tool that I always turn to when I'm stuck. I hope that you find these tips and tricks useful, and remember, whenever you're in the weeds, put a binding.pry in it!