In the previous post, we enjoyed playing around with Byebug, The de-facto debugger for Ruby on Rails Rails. This post for Beginners’ Guide To Pry Gem For Debugging RoR Applications, will dwell on another debugging technique of debugging ruby on rails applications. Before getting into pry, As we know REPL, REPL stands for Read-Eval-Print-Loop. It’s a program that allows you to type Ruby code & see the result directly. IRB is the built-in Ruby REPL that every Ruby developer is familiar with.
Debug Using IRB – Interactive ruby shell
How can you use IRB?
Interactive Ruby Shell. You can open IRB by typing IRB inside a terminal window.
Then you can write your Ruby code & press enter to run it.
If you want to close IRB you can type exit.
What is Pry?
pry is a type of REPL, So IRB is a REPL, why should we bother with Pry? Well, there are a number of key features that make Pry a more robust and powerful tool than IRB, but the main reason comes back to the ability to get right inside of your code and see exactly what’s going on at a given time. Think of it as freezing time and being able to experiment with all of the objects around you, one at a time, in the context of their environment.
gem install pry
Setting up Pry
To set up Pry in your code, drop in require ‘pry’ at the top of your file and a binding.pry, or a breakpoint, wherever you want your code to stop for testing.
Finally, to exit Pry, simply type exit or !!!
Benefits of Using The Pry Gem
- syntax highlighting
- Autocompletion (irb has some, but very basic)
- Introspection commands (list methods, variables, constants, etc.)
- Editing code you typed before & running it again
- These features make pry a better choice most of the time, so give it a try if you haven’t yet.
If you’re using Rails, you can enable pry for the rails console by adding pry-rails to your Gemfile.
This also adds the show-models & show-routes commands.
Pry has many configuration options, you can set them in a .pryrc file in your home folder for global settings, or on your project folders for local settings.
Hope you have enjoyed reading this mini-article on Beginners’ Guide To Pry Gem For Debugging RoR Applications, happy debugging RoR applications.