Using Pry for More Efficient Debugging
We were in lecture here at the Flatiron School the other day, and Avi mentioned something called a REPL. He also mentioned that one of our favorite debugging tools, Pry, had a lot of cool functionality that we don’t often exploit, so I decided to dig around a little bit and see what I could find.
What is a REPL? The acronym itself stands for “read-eval-print-loop,” and describes an environment where you type in code and get interactive feedback immediately. This type of tool is also known as an interactive top-level or language shell. It’s super easy to use for beginners, which is part of what has made Code School and Codeacademy so successful. No text editors, just instant feedback. For example, while making my Tic-Tac-Toe, I used this online REPL extensively. When you’re at the earliest stage of learning to code this is a great tool. However, REPL’s are also very useful for full-fledged developers.
1 2 3
We also have experience with REPL’s – such as our friend IRB (Interactive Ruby!). IRB is pretty cool for interpreting your Ruby on the spot, and it’s definitely useful, but it also doesn’t really have too many features. And sometimes, it’s a little visually confusing; anyone who has ever tried to define a method in IRB knows that it messes up the indentation a bit, for instance. There’s definitely no syntax highlighting, and it’s not really that useful from a debugging standpoint. In order to access the methods from your actual program, for instance, you’re often stuck copying and pasting, which gets funky pretty quickly. You’d BETTER have converted from tabs to spaces before that. And also, you get annoying (“Do you really want to display all 9423 items?” anyone??”)-type messages.
Pry is d0pe. It works as a powerful alternative to the standard IRB shell for Ruby. It features syntax highlighting, a flexible_plugin_architecture, runtime_invocation and source/documentation_browsing! Us Flatiron School Ruby 005’s have already used it for debugging purposes, but it can do a whole lot more.
Pry is super easy to install:
gem install pry does the trick. Like IRB, Pry can be invoked right from the command line. To start it, just type “pry”. Then you will be in something that looks and feels like IRB, but is better in a lot of ways! For example, all the indentation works well, and you have syntax highlighting. You’re able to customize all of this as well in your “.pryrc” file. This is analogous to the IRB “.irbrc” file. For example, here is a way to make it so that your pry output uses awesome print:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
TONS OF AWESOME FEATURES
First, you can get to the entire Ruby documentation from inside pry. This is really awesome, because it means that we can be lazier and avoid the treachorous Google path and not have to leave our shell. For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
Pretty cool, right? And for those people who are curious how a method based in C works, you can also get to the source code of the method really easily!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
Wow!! And you can also totally get line numbers with the
You can also get a Github gist of any method! Once you install the
gist gem, you can totally generate a gist with one command:
Sweet. Github just created a Gist of your code.
Pry supports a bash-like method of navigation. When you’re in Pry, you can type “whereami” at any time, and it works a lot like the pwd command in bash. You can type ls to see the directories and files in your current level of Pry, and can even “cd” into them.
So let’s actually use it a little bit. Here, we’ll use it to help improve our Sailor model.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
And then your binding stops your code in its tracks! It keeps track of your line numbers and stops you exactly where you want it to.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
Breakdown of what just happened
binding.pry stopped me in the middle of my code. Now I can run
ls as if I were in a bash terminal, and I can also use the
-M option to specify that I want to see all the methods of the class. Wow! It returns them right there for me to see (Note: if you do this in Rails, you’ll realize how much the framework gives you, it’s nuts).
2) Oh cool, I can instantiate new instances of my Sailor class right there on the spot; I don’t need to copy and paste my class into my REPL and waste time. We don’t have a console in this application, so this is very useful.
3) Lets try exercising the full power of our sailor, Arel. When we call
say_hello_to_everyone on our swabber, we get an error. It looks like there’s a breakdown in line 11: there is no method
put for Arel. Well, that makes sense, because it was a typo!
4) If you type in
wtf anywhere in pry, it returns the last error message that was logged (it keeps track of your history, accessible with the
5) Wow, so I need to edit that “say_hello_to_everyone” method. But I can totally just do that by typing in
edit say_hello_to_everyone. And then you know what it does? It sends me right over into Sublime, right to the CORRECT LINE where the method is defined. HOW COOL IS THAT?
So pry is pretty awesome – at the minimum it’s better at doing most of the things you need than IRB. And I know what you’re thinking – “it would be so awesome to include this in my rails projects and run pry instead of the rails console.” Guess what: You TOTALLY CAN!
Add this line to your Gemfile:
Once you update your Gemfile and run
bundle install, you’re ready to enjoy Pry and all of the benefits it brings. You’ll be well on your way to understanding how your code works and debugging uber efficiently, without headaches, and with color syntax highlighting :)
Until next time, enjoy Pry!