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.
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.
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.
At this point, we're able to check whether '@user' is set to the right user id.
We can also check whether our user is logged in:
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:
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:
And that's it! You're up and running with Pry!