Intro to Pry — The Short Version
While I have never been formally taught how to code until fairly recently, I’ve dabbled in it over the years quite a bit — playing in Google Apps Script, building simple web sites for friends, making my emails prettier. I had never used any sort of debugging tool prior to about a month ago. At which point, I had taken a free Google Apps Script course (from Ben Collins — definitely check it out if you’d like a quick intro) and learned about Logger.log.
Stack Overflow has, of course, been my friend for years, but I distinctly remember being frustrated anytime someone referenced using Logger.log. i.e. “Have you tried using Logger.log?” What kind of response is that? At the time, it seemed this unconquerably large unknown and I didn’t want to try to learn it because it would take too much time— now I use it constantly. It was a lot simpler to use than I thought it would be, and so is pry. At least at the most basic level — which is all you really need to learn at first.
So here’s the short version:
Step 1 — Install it.
You can check if it’s already installed by using this in your command line:
gem -i list pry
It will return a boolean. If it’s not installed, use this in your command line:
gem install pry
Step 2 — Require it.
At the top of your code, write out the line:
Step 3 — Insert it.
Use this as many times as you’d like in your code:
Place it at a point or points in your code. To start, just pick any spot, so you can play with it and see how it works. As you go, you’ll find where exactly you want to place it when you run into various logical errors.
Step 4 — Run your code.
Pry stops the code at the point(s) where you placed binding.pry. At each point you can type in various variables and see what’s going on in your code at that particular spot.
Step 5 — Exit.
Exit the pry session by just typing exit. Exit the whole program by typing exit-program.
That’s all, folks! It’s super simple. There are other add-ons and such you can use, and more to learn about it, but this will get you over the “THIS IS SOME WEIRD THING I DON’T KNOW ANYTHING ABOUT” hump.
Code well, my friends!
Top comments (0)