Why Pry?

Cristina Cristobal
5 min readApr 18, 2019

--

If I could share a piece of information with someone learning to code in Ruby, I’d tell them this — learn how to use to pry and learn it early. I learned this lesson the hard way after fumbling through multiple layers of data and code.

When working in Ruby, calling “binding.pry” will pause your program at the line in which pry is called. At this point, you will be able to test the code you’ve written as well as read through any data you’ve been given.

To help demonstrate the utility of binding.pry, I will pull from a lab titled “The Bachelor” that I completed as a part of my pre-work for Flatiron School’s Software Engineering Bootcamp.

This lab requires you to create methods using its nested dataset. The method I will use in this example returns the average age of The Bachelor contestants for any given season.

First, let’s look at what this dataset looks like.

So far, we can only see the data for two contestants from season 19. For each contestant, we have five keys and their associated values. Let’s see how many seasons of The Bachelor for which we have data so we can get a sense of what’s going on.

Holy moly! There are 2,054 lines of data we have to work with. In order to find the average age of contestants for any given season, we’ll have to iterate through multiple levels of data, collect the age of the contestants, and then find the average.

As you may have noticed, the data we’ll need to collect lives in different levels of a giant hash further refined by season, then keys, then values. To keep track of these levels, let’s use a pry!

*Note: When using pry in your code, make sure to call “require ‘pry’” at the top of your program. Then enter “binding.pry” where you would like to test your code. Additionally, to successfully hit the pry, you will need to call “ruby <<filename>>” in your terminal.

On line 6 I’ve called my first pry. This will return the data for each key and its associated values when I call on the variables in the block. For example, if I type “s_num” I get the following output:

Great! This tells me that each “s_num” is a season of the show.

Now, what’s “contestants”?

Ah, so “contestants” is an array of the contestants information (in this case, the contestants in season 19).

We want to collect the ages of these contestants, so we’ll need to further iterate through contestants to grab those age values. Let’s continue the code and pry again to check our data.

Here I’ve called pry on line 9 which now gives me access to call on “cont_keys,” “cont_values” and any of the other variables/methods called upon before this line.

When calling “cont_keys” the terminal, I get this:

I’ve now gone into the array which housed all of season 19’s contestants. This gives me access to the contestants’ keys (name, age, hometown, occupation, and status) and their associated values.

We know that we’ll want to collect the ages of these contestants, so let’s make sure we’re grabbing the right information.

By calling “cont_keys[“age”] in my pry, I’m testing that this data exists and provides the appropriate return, which as I saw in my previous pry is the age of Tessa Horst — 26!

At this point, we can even test if Tessa’s age was shoveled into the “ages” array by entering “ages” into the terminal.

It did! We’re so close to finding the average ages of these contestants.

Here, I’m calling pry on line 13 which should give me access to everything defined prior to this line.

Let’s check if calling on “ages” returns an array of ages of all of the season 19 contestants.

It sure did. The array is even so long that it can’t be contained on my screen.

Finally, we can check if my “averages” method worked.

Ha-ha! It did!

Let’s look at that code one more time:

Using pry, we’ve been able to test the return of each of the variables defined in the method “get_average_age_for_season,” as well as check if the overall method returned our average. Thank you, pry! I’ll be seeing you very soon!

--

--