While working through the pre-bootcamp coursework provided by Flatiron School, I constantly found myself struggling to check and debug return values within iterations and nested data structures. The Learn.co curriculum introduced a tool called Pry, but I had trouble understanding how to implement it in my code. It wasn't until a difficult lab by the name of Hashketball that things finally clicked. After struggling through the lab for far too long, I finally forced myself to read through Pry's documentation and my ability to write and debug hash iterations immediately improved.
If you ever find yourself struggling to build out a Ruby program, Pry could prove to be an invaluable tool in your arsenal. Pry is a powerful tool that Ruby developers can use to debug programs and push past hurdles. Like other Ruby gems, the Pry gem must be required in your environment settings or specific ruby file in order to access its functionality. Additionally, there are several Pry plugins, including pry-doc and pry-byebug, that expand the gem's features and capabilities. You can think of these plugins as separate gems that must be required in your gemfile.
Once you have configured your program with the Pry gem, it's time to pry open your code and experience the joy that is debugging with Pry. Pry is packed full of features but the following examples highlight what I believe to be the most useful.
REPL (Read, Evaluate, Print, Loop)
Pry can replace Ruby's integrated IRB shell and builds on IRB's REPL functionality with some additional features. Both IRB and Pry allow you to execute Ruby commands in the terminal and receive immediate results in the terminal. Having REPL functionality in your terminal is a very useful tool that allows you to quickly test Ruby code without running your entire program. Let's say, for example, that you want to iterate over an array and see what values are returned. You can simply enter the array, use an iterator on that array, and check your results:
Notice that while both IRB and Pry returned the same values for this simple iteration, the way Pry prints to the terminal looks a bit different. One of Pry's advantages over the standard IRB is its use of syntax highlighting. Other features that give Pry the edge over IRB include:
Tab completion
Debugging tools
Ability to view documentation
Runtime invocation
Perhaps the most useful feature of Pry is its ability stop a program's runtime in its tracks. The Ruby programmer can invoke the pry console during runtime by inserting the line 'binding.pry' wherever they would like to stop the program. When the interpreter hits the binding.pry, Pry will open a REPL session in the console, allowing you to test variables, return values, iterations, and more.
The following is an example of the utility of Pry when it comes to dissecting nested data structures in your programs. I recently contributed to the development of a dev job search CLI that relied on a database of job openings seeded from the GitHub Jobs API. The API data was structured in a JSON hash and our database tables were seeded with values from these nested hashes. The following image shows how we populated the companies table for this project:
When populating a database table, it's vital that records going into the table are created with the correct data! How might we check our iteration values to ensure that everything is working as expected? The answer is Pry. Inserting a binding.pry near the end of the "populating companies" block allows us investigate return values by stopping the runtime and entering a REPL session:
Once the binding.pry is inserted into the block, the code must be executed again in order to open the Pry console. When the Pry console opens in our terminal, we are within the block that is populating the companies database table and we are now able to test variables and return values:
Now that we have pried open the "populate companies" block, it's time to check some values! When 'job' is inserted into the pry console, a hash for a single job opening is printed to the screen. The job hash has several keys including type, url, company, company_url, location, title, etc. To make sure the correct values are being sent to the database, we can enter the following commands into the Pry terminal:
Once we have determined that our iteration is functioning as expected, the binding.pry can be removed an our table will populate upon the next execution of the program!
Documentation and source code browsing
Let's face it, being a developer means you will be relying heavily on Google. I often turn to the web to research Ruby methods and scour documentation to ensure that I am correctly structuring syntax. What if I told you that Pry offers a plugin that allows you to browse documentation right in your console? Welcome to the pry-doc gem. Pry-doc enables you to read Ruby documentation within your current Pry console session. Now for an example. Let's say I can't remember how the .find method works on a hash. I can enter the following command into the console, and voilà! The Pry console prints the documentation for that method in the terminal:
Hooray! Now you can access useful documentation in your terminal without relying on Google!
Useful Pry Commands
pry -Opens the Pry console in your terminal
binding.pry -Stops runtime and opens Pry console
exit -Exits current loop
exit! -Exits Pry console
whereami -Prints current location within program
show-doc -Displays documentation for a class or class method(s)
history -Prints history of Pry commands to the terminal
ls -Prints the variables and methods in the current scope of the program
help -Prints the current list of available Pry commands
Pry drastically improved my ability to debug Ruby programs and is the tool that I always turn to when I'm stuck. I hope that you find these tips and tricks useful, and remember, whenever you're in the weeds, put a binding.pry in it!
Top comments (2)
Is it possible to jump to a specific loop in pry instead of having to 'exit' each time?
Using comand next