Read-Eval-Print Loop: What are REPLs and How to Use Them
Testing out the viability of new code can be a pain. While debugging tools and test suites can help, sometimes you’re just trying to decide between a few different approaches to tackling a problem. This is one of the places a REPL can help.
REPL stands for read-eval-print loop. That is to say that a REPL will take input (read), run those commands/code (evaluate), and print out the results, all in a loop. This basically describes most text-based shells, but here we’re referring specifically to ones that use a specific programming language.
If you’ve used a command prompt like Bash or PowerShell, you’ll be familiar with the basic concept. The bulk of what you write in a programming language REPL will just be valid code in that language. There are usually ways to load libraries or your own source into the environment so you can play around with code you’ll need in a larger project. Some REPLs also provide additional commands for introspection and manipulation of entities in your environment.
There’s some form of REPLs for virtually every language you’d be working in, but I’ll list some popular ones here, sorted by language.
Ruby ships with a default REPL called irb, short for “interactive Ruby”. You can run
irb to start the shell. You’ll be provided with a prompt, at which point you can start running Ruby. You can quit with
exit or by pressing
ctrl+D on a blank line.
Irb doesn’t provide a whole lot of functionality beyond interpreting code, which is why I highly suggest using Pry.
Pry is a gem that can be installed with
gem install pry. It can also be added to your Gemfile and installed via bundler, though this usually should be flagged a development dependency rather than a production one. Then it can simply be run with
Features of pry include:
- syntax highlighting
- tabt completion (press tab while typing)
- paging of long output
- list methods of objects
- search for methods on all loaded objects
- ability to view, edit and reload code
- ability to move the evaluation context inside an object
- pry help and rdoc support
Additionally, pry can also be used inside of a large application as a sort of interactive breakpoint. Essentially, you can start a full pry session anywhere in your code with
binding.pry, with which you have the ability to view, change and edit values, monkey-patch code, investigate errors, etc.
The Python interpreter itself, run as
python with no input or filename, can run in interactive mode. Much like irb, it’s nice to have with every installed Python version, but it’s not a very helpful version.
IPython can be installed with
pip install ipython and run with
ipython. It is a much more full-featured REPL with more features.
Similar to Pry, you can use iPython as a sort of interactive debugger by including the line
IPython.core.debugger.set_trace() where you’d like to stop and take a look around.
Not quite a REPL, Jupityr Notebooks are a sort of live multimedia document which can contain executable Python (among quite a few other programming languages). They’re often used for math and science applications and can include helpful graphical components like graphs or visualizations. Find out more at the Jupityr website.
Mono is an open source implementation of the .NET platform which predates Microsoft’s release of .NET Core. With it comes the
csharp command which is a C# REPL.
This REPL is somewhere in the middle as far as features, but it does feature tab completion and a few features, such as
help to see more information.
Installing XCode’s command line tools will install the
swift binary, which, if run with no arguments, will enter into a Swift REPL.
Somewhat similar to Jupityr Notebooks, XCode can create a Swift Playground, which is an executable document which can include code as well as some graphical components.