Ruby’s binding.pry: A beginners guide
Are you new to learning Ruby? If so, learning Pry & how to read errors will help bridge the gap. If you’re like me, I’m the I like to work smarter not harder type. Pry helps for efficient programing & beginner friendly learning.
Thankfully, I’m not one to give up so I rolled up my sleeves and did some google searching and tried again. After some more practice it clicked! I then asked myself why was this so hard to learn? In this article I’m going to explain Pry and how to better understand it’s capabilities for an early on developer.
What is PRY?
“Pry is a powerful alternative to the standard IRB shell for Ruby. It features syntax highlighting, a flexible plugin architecture, runtime invocation and source and documentation browsing.” — source
Pry is another Ruby REPL with some added functionality. REPL stands for Read, Evaluate, Print, Loop. It is an interactive programming environment that takes a user’s input, evaluates it and returns the result to the user.
What does this mean? Pry is essentially your right hand man when it comes to debugging & reading errors. Before you learning more about Ruby, I would first suggest mastering pry. It is a fundamental tool in helping you understand the Ruby language.
How does Pry work?
First, In your terminal check to see if pry is installed:
binding.pry is a single line of code you can place inside any method to pry into the code block & review results. Let’s get this set up.
- Inside yourfilename.rb file on line 1 type — require ‘pry’
- Require ‘pry’ must be at the top of any yourfilename.rb files
when calling on binding.pry - In your terminal type ruby yourfilename.rb to run the file
Using binding.pry inside a method
binding.pry is a single line of code you can place inside any method to pry into the code block & review results. Let’s get this set up. Inside the pry you can try out new methods till you get the desired results.
Tip: To use binding.pry in a method you must use do..end syntax to write your methods, you can change them back to {} afterwards if you so choose.
In the example below, we can now loop through any collection of data to see the elements we want to return. Typing exit will loop to the next element in the collection. We have three elements in our collection we will need to type exit three times to see all of your returned results.
Tip: If you need to exit of a loop type !!! (triple bang) to hard exit
the pry console.
Wait!! Pry isn’t working? binding.pry needs to be placed inside the code block to invoke the pry console in the terminal. After the code block means the code has already run and there isn’t anything to pry into. Move your binding.pry to inside your code block & run your file again.
Using binding.pry twice in the same file.
- Type your first binding.pry inside your method
- Type your second binding.pry as your second to last line of code. Put a 0 after it so that pry isn’t your last line of code. This is to ensure pry works on this file and doesn’t jump to another file.
- Run your file & you should see see binding.pry and the last line of code.
Now, how do I to get to the pry inside the method?
Inside the pry console type the name of the method along with any arguments and pry will jump to the method holding binding.pry.
Pry commands:
exit — has two uses
1) leave the pry console
2) “soft exit” to move to the next element in the loop
!!! — hard exit or triple bang
Use to exit any iterations and/or to hard exit the pry console.
How to troubleshoot Pry?
Read the errors. It sounds simple, I know, but your next task is to become comfortable reading the errors. Pry returns errors that help guide you in a time saving direction to get your code working.
Lastly, why is Pry so important?
For myself pry was crucial in learning how to read errors & know where they are so I can find them, fix them. It’s a real time saver & informative tool. Pry takes the guessing game out of coding. Instead of spending x amount of time on one problem that could be fixed with using pry from the start.
Remember if at first you don’t succeed then pry, pry again!