Why I Pry
title pun courtesy of The Magnetic Fields
What is IRB?
IRB is what's called a REPL, or read-eval-print loop. It takes individual inputs, and returns their results. It is accessible through the terminal, and is useful for experimentation in real time. It comes packaged with a default Ruby install, and there's even a version of it available through your browser.
What is Pry?
Pry is a Ruby gem that is also a REPL, but significantly more robust than the IRB. Most notably, you can start Pry inside a running program. This allows you to a) debug a program as it runs and b) experiment with the program while retainin access to all the variables, classes, methods, etc. that you've already defined, without having to copy-paste into your Terminal nearly as much.
Filesystem-style Commands
"Pry commands are not methods; instead they are special strings that are interpreted directly by Pry before the input buffer is evaluated."
-from the Pry wiki
Pry commands allow us to manipulate things other than the expression we are trying to REPL. Here is an example I would have found extremely useful in previous labs, had I known it existed.
We can use "ls" to show a list of variables and methods in the current scope.
We can use "cd" to enter (and experiment within) a different scope.
We can use "show-source" to show the code that created the method/variable whose scope we are in.
We can use "edit" to open that code in our default text editor.
For example: I ran binding.Pry inside the creation of a new TicTacToe game. I used cd to get into the new game, then cd'd into the current (empty) board. Then, I used show-source, and it showed me the entire contents of lib/board.rb. (I've used a partial screenshot here for space's sake.)
Then I typed "edit board" which opened board.rb in Sublime Text. As an experiment, I decided that board.reset should give us an array of 9 "z"s, rather than 9 " "s.
Once I saved my changes and closed board.rb in Sublime, I ran show-source again, and it showed the revised contents.
Not only did it show me the edits, but the edited code is now useable in the current Pry session.








