Pry Your Ruby Worries Away
The start of a wonderful friendship.
When I first began coding, errors were my worst nightmare. There were countless times where I would confidently write over 100 lines code just to get a big red error message in return. However, I soon realized that errors come naturally. And In addition to learning how to code, you have to learn how to debug your code efficiently.
At Flatiron School, you are given labs that contain tests you need to pass in order to submit your work. I relied heavily on rspec to guide me and help me pinpoint where I messed up in my code. It wasn’t until the practice code challenge that I realized that relying on those test was a mistake because in the real world, I don’t have prewritten tests to verify my code. I would need to test the code myself!
Debugging is an unavoidable and important part of being a programmer. It can be scary at first but don’t worry! As ruby programmers, we have a handy dandy sidekick called Pry.
What is Pry?
Pry is a Ruby gem that allows you to freeze your program and inspect the code inside. It is a REPL (Read-Evaluate-Print-Loop) which is a programming environment that evaluates a user’s input and returns a result. We can use Pry to verify and understand exactly how our code works at any given moment.
Why use Pry?
If you are familiar with Ruby, you are most likely familiar with IRB (Interactive Ruby), which is Ruby’s own built-in REPL. So why use pry, if we can just use IRB instead? Well, pry goes beyond its’ functionality of REPL commands.
Advantages include, but not limited to:
- syntax highlighting — Pry color codes syntaxes for better understanding.
- live help commands — Pry provides a list of commands with descriptions.
- flexibility — Pry allows you to navigate your code in different scopes.
How to use Pry:
Step 1 — Install Pry
Installing Pry is as easy as typing the following line into your terminal:
gem install pry
Step 2 — Require ‘pry’
At the top of the file that you wish to use pry on, you can simply type in require ‘pry’
. Here is an example:
Once thats done, you are ready to start testing out and verifying your code!
Step 3— Freeze Time with ‘binding.pry’
In order to fully show you how to use ‘binding.pry’, I written out my own example code. Here we have a Pet class that contains 3 methods: an initialize method, a self.all method (which will list all instances of that class), and a self.find_all_dogs method (which will return an array of all dogs).
As you can see, we have a require ‘pry’
at the top of the file that we will run (the main.rb file). In the this file, we initialize a few instances of different types of pets and lastly, we call a Pet.find_all_dogs
method to select all the pets who are dogs. When this method is called, it will run the method from the Pet class. In the second picture, on line 18, I added a binding.pry
. When I try to run my file now, this command will freeze my program in the middle of executing that method. (Remember to always call a method first and then add a binding.pry
)
Once inside my method, I can examine what is inside my method. I can ask what the block pet is referring to:
In return we are given the first instance of class. This makes sense because the method is going through each of my instances and using that instance in the conditional in line 19. This is important because within this method or within this scope, I am shown that pet represents the current instance that is being passed through the enumerable. If I were to type continue in pry, it would move on the next instance.
Pry also provides some helpful commands. In the pry session, I can call the ls
command to identify the scope of the class and the method.
This command list out all the class and instance methods as well as the class variable. In the local scope, pry tells us that we can access the variable pet. This is available to us because we are still inside our Pet.find_all_dogs
method. We can call these methods within our pry session!
Another neat command is whereami
. After running different commands and testing your code, you might get lost and forget where you are. The whereami
command will always remind you.
whereami
command in action.These are few cool things that you can do with pry. As a beginner it is important to learn and understand how to use pry. Not only will it help you find your errors more quickly but it will help you understand your code better in general!
Happy Coding!
Helpful Links
Official Wiki for Pry (Includes Guides!) — https://github.com/pry/pry/wiki
Pry on Github — https://github.com/pry/pry
Pry Website — http://pry.github.io/