Using Pry for Object Orientation!
Intro:
My first encounter with Pry was during the time that I was completing the pre-work course for Flatiron School. At the time, I wasn’t really interested in learning about Pry not only because I couldn’t understand it, but I already had IRB and repl.it for testing out my code. Little did I know Pry was actually one of the most powerful debugging tools I could ever use. I was capable of familiarizing myself with Pry during my second week at Flatiron School when we were learning about OO (Object Orientation). Although IRB and repl.it are very much useful tools, Pry was the main reason why I was able to pass my first code challenge.
What is Pry?:
Pry is defined as an interactive ruby shell that starts a REPL within a program that’s running. Letting developers debug and make changes within the current system. REPL stands for:
R ==> Read
E ==> Evaluate
P ==> Print
L ==> Loop
When going over Pry’s functions, it sounds very similar to IRB’s purpose. The main difference between IRB and Pry is that IRB doesn’t allow the programmer to test in the middle of a method or loop. As coding becomes more complex, IRB or websites such as repl.it won’t come in handy. Let’s take a look!
In this picture I’m using IRB to test if all of the methods within my Patient class are functioning the way they’re supposed to. This way is already problematic because I had to copy and paste the entire class. What if I made a grammatical mistake? Or what if I needed to test other classes? I would also have to give variables a value so that I can test out the methods. Using IRB takes away too much time and that’s when Pry comes in.
Installing and Setting up Pry:
Similar to other Ruby gems, a Pry gem needs to be installed in order to be used. All a user has to do is type gem install pry
on their terminal. Once installed, you can either create a new Ruby file just for testing or you can use the same file in which you are writing your code. Either way you’ll have to type require ‘pry’
at the top of the file and then add binding.pry
at the bottom or wherever you want your code to stop for testing. In my example, I created a test.rb file that allows me to test everything.
Here we have the require ‘pry’
along with all of the other files that I want to test placed all the way at the top left corner. Also, we can place the binding.pry
command right below our set variables since that’s where we want our code to stop. In our terminal, we have to type ruby test.rb
in order for Pry to work. Here’s an example:
Once you’re done with that, then you can start testing with the variables that you created and also see if the methods that were created are functioning properly.
Using Pry to Test Methods:
The examples that I’ve been showing so far are related to Object Orientation, specifically many to many relationships. I have a Doctor, Patient, and Appointment classes, but I’ll demonstrate how to test each method within a Doctor class.
Let’s start with the def self.all
method! This method is considered to be a class method which is trying to show us a list(array) of all doctors in that class. Whenever you want to test a class method, for example def self.all
, you should always use name_of_class.all
. If we type Doctor.all
in our pry console, we should be expecting an array of all the doctors. Here’s an example:
This is correct! Although it did return just two doctors, Those were the only doctors I created for testing anyway. In order to test def patients
, I will no longer use Doctor.all
because it is not a class method. def patients
is known as an instance method. To test an instance method, I have to use a variable that corresponds with the specific class. For my specific problem, dereck
and meredith
are my only variables(only doctors).
To call on the def patients
method, all we have to do is type dereck.patients
in our Pry console. This method is expected to return an array of all the patients that dereck(the doctor) has. Let’s see if we were successful!
Good! This means that my patients method within my Doctor class is working. If you have more methods to test out then you can follow the previous steps which shows examples on how to test class and instance methods.
Conclusion:
One of my main issues with Pry was not knowing where to add the binding.pry
. It’s really helpful to understand what you’re doing in your code and what exactly you would like to test. Getting used to Pry can be a challenge, but once you get the hang of it, you’ll be using it for EVERYTHING!
Additional Resources: