'Splain Like I'm Five: Ruby REPLs and Pry
Tags: beginners, programming, ruby, codenewbie
Published: 2022-03-22 04:00:22

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's REPLs**

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. 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/th1slbe5c3l4l5mjmakd.png)
**PRY**

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:

1. It can loop through code and pause at intervals of user-specified subsections of code. 
   
2. Interact with and test-edit your code without ever leaving the Pry programming environment. 

<u>"Runtime Invocation" with Binding.pry</u>

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. 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c0zeq7g82jb078pmh76a.png)

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.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xzg43n7x7hn9sg9gko7g.png)

<u>Command Shell Integration</u>

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 ](https://www.rubyguides.com/2019/02/ruby-rake/) directly from Pry!  

<u>Other features of Pry:</u> 

- Syntax highlighting

- 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](https://github.com/pry/pry/wiki/Customization-and-configuration).

**You Didn't Hear It From Me**

Additionally, you can check out [this video](https://www.youtube.com/watch?v=4OinPTpt3I8) 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](https://www.geeksforgeeks.org/monkey-patching-in-ruby/).

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](https://cdragon.medium.com/make-your-own-ruby-repl-from-scratch-2b0677b4b291)_! 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](https://www.sitepoint.com/pry-a-simple-start/#:~:text=To%20start%20pry%2C%20simply%20type,the%20Rails%20console%20with%20Pry.)! 

Give it a pry! 

**Resources**

_[What is a REPL in Ruby? (IRB, Pry, and More...)](https://www.rubyguides.com/2018/12/what-is-a-repl-in-ruby/)_

_[Debugging with Pry ](https://learn.co/lessons/debugging-with-pry)_

_[Ruby Docs: Pry](https://www.rubydoc.info/gems/pry#edit-methods)_

_[Pry Irresponsibly - Things You Shouldn't Do But We Will Anyway](https://www.youtube.com/watch?v=4OinPTpt3I8)_

_[Monkey Patching in Ruby](https://www.geeksforgeeks.org/monkey-patching-in-ruby/
)_

_[What is Rake in Ruby & How to Use it](https://www.rubyguides.com/2019/02/ruby-rake/)_

_[Make your own ruby REPL from scratch](https://cdragon.medium.com/make-your-own-ruby-repl-from-scratch-2b0677b4b291)_

_[Pry: A Simple Start](https://www.sitepoint.com/pry-a-simple-start/#:~:text=To%20start%20pry%2C%20simply%20type,the%20Rails%20console%20with%20Pry.)_