Welcome to the Back-End! If you're reading this Ruby blog, you've likely made your way here through front-end web development.
"Ok, front end was cool--I had a nifty browser console in which to test my code. Code goes in, changes on the browser come out! Easy-peasy!" -You, probably.
Well, now you're in back-end. Sunlight in the form of a browser console doesn't reach back here. You're now working in terms of creating and passing data to and between databases, and none of those tasks require a browser to write or operate. So how do we test our code??
The simple answer: REPLs -- or Read, Evaluate, Print, Loop programming environments. REPLs are an interactive terminal shell which takes a user's input, evaluates it, and returns the result to the user. REPLs are a back-end programmer's most-used and most powerful tool for debugging and modifying code in real-time.
Ruby installs with its own built-in REPL called IRB ("Interactive Ruby"). You can enter the IRB environment by simply typing "irb" in your terminal. You're now in a "sandbox" programming environment where you can test your code functionality. The unfortunate thing about IRB is that it's not connected with your actual program in any way-- it simply tests the code, then you have to exit IRB and retype the correct code in to your text editor or through terminal in order to add it to your program. In other words, it's a bare-bones testing REPL that has no interactivity to your program.
PRY is one of many third-party REPLs in the form of a installable Ruby gem. It, too, creates an interactive terminal programming environment with which to test Ruby code, only with tons of extra perks. The two main perks of Pry that IRB doesn't have are:
It can loop through code and pause at intervals of user-specified subsections of code.
Interact with and test-edit your code without ever leaving the Pry programming environment.
"Runtime Invocation" with Binding.pry
Pry is, at its core, a powerful debugging tool. It utilizes a Ruby class, Binding, which captures the objects-- specified by its placement-- for use outside of the program (aka in the REPL environment). You can then "pry" in to that specific bit of code and inspect it. It works by adding the line "binding.pry" in to any (or multiple) parts of your code.
As the line is invoked during runtime, it will freeze your program and open a REPL environment so you may inspect that specific line of code within the scope it was called. If you, for example, place a binding.pry in the middle of a method, when the program runs your code it freezes upon the binding call and enters a REPL so you can explore the data within that method.
Command Shell Integration
Pry commands can be forwarded to the command line with the "." preface. This means you can navigate your file system, launch editors, run git commands, and utilize Rake directly from Pry!
Other features of Pry:
Autocompletion (IRB has some, but very basic)
Introspection commands (list methods, variables, constants, etc.)
Documentation Browsing using pry-doc
And much more! Pry is fully customizable, so check out a full list of pry plugins here.
You Didn't Hear It From Me
Additionally, you can check out this video about some Pry hacks that are, on paper, not considered good practice... For example, editing your Ruby classes in-line using Pry. Yes, it's that powerful! But doing so does verge on the frowned-upon practice of "Monkey Patching" which you can read more about here.
I will say, taking 10 minutes to dive in to multi-line editing with Pry helped me understand how Pry works in the grand scheme of the command line and the program as a whole, so I found it helpful for context. You didn't hear it from me, though...
The Many Other Ruby REPLs and More:
Outside of the two most used Ruby REPLs--IRB and Pry-- there are countless other Ruby REPLs on the market, all with their own functional focus. This is probably because you can actually make your own Ruby REPL! Many folks have, and left them available on codebases like GitHub for public use.
Pry, though, remains the industry standard for debugging Ruby code in real-time. Pry is the best because you can freeze your code and debug at intervals of your choosing, interact with your code inside the REPL, and it has all kinds of other handy features and plug-ins to customize for your use! For a quick tutorial on how to install and use Pry for yourself, check out this article!
Give it a pry!
What is a REPL in Ruby? (IRB, Pry, and More...)
Pry Irresponsibly - Things You Shouldn't Do But We Will Anyway
What is Rake in Ruby & How to Use it
Make your own ruby REPL from scratch
Top comments (0)