RubyGuides
Share this post!

What is A REPL in Ruby? (IRB, Pry & More)

REPL stands for Read-Eval-Print-Loop.

It’s a program that allows you to type Ruby code & see the result directly.

One popular REPL is irb.

Another is pry.

They’re useful because you can quickly test how some Ruby code works.

For example:

If you’re trying to convert an array of strings into an array of integers.

You may not remember exactly how to do that

You look it up (which is perfectly fine!).

And try it out in your REPL until you get it to work as you want. Then you can integrate this code into your project.

The big benefit?

You can test out a little bit of code by itself, so if it doesn’t work you have less moving parts, and you can more easily find out how to fix it.

It lets you focus on one thing!

Now:

Let’s review some REPLs you can use with Ruby.

Interactive Ruby Shell (IRB)

IRB is the built-in Ruby REPL that every Ruby developer is familiar with.

It doesn’t have many features, but it does what it’s supposed to.

You type in code & you get the results back.

How can you use irb?

You can open irb by typing irb inside a terminal window.

This is what you should see:

irb(main):001:0>

Then you can write your Ruby code & press enter to run it.

If you want to close irb you can type exit.

You can enable the command history by creating an .irbrc file in your home folder.

With this content:

IRB.conf[:SAVE_HISTORY] = 500

Add this too if you want to enable auto indent:

IRB.conf[:AUTO_INDENT] = true

Or customize your prompt like this:

IRB.conf[:PROMPT][:CUSTOM] = {
  PROMPT_I: "irb(#{Dir.pwd}) ",
  PROMPT_S: "irb(#{Dir.pwd})* ",
  PROMPT_C: "irb(#{Dir.pwd})? ",
  PROMPT_N: "irb(#{Dir.pwd})* ",
  RETURN: "%s\n"
}

IRB.conf[:PROMPT_MODE] = :CUSTOM

Benefits of Using The Pry Gem

Pry is a REPL & a Ruby gem you can install anytime you want.

It’s like irb but it has more features.

Features like:

  • Syntax highlighting
  • Autocompletion (irb has some, but very basic)
  • Introspection commands (list methods, variables, constants, etc.)
  • Editing code you typed before & running it again
  • Debugging

These features make pry a better choice most of the time, so give it a try if you haven’t yet.

If you’re using Rails, you can enable pry for the rails console by adding pry-rails to your Gemfile.

This also adds the show-models & show-routes commands.

Pry has many configuration options, you can set them in a .pryrc file in your home folder for global settings, or on your project folders for local settings.

Ruby -e

If you only want to run a single line of Ruby code without launching a REPL then you can use Ruby’s -e flag.

Here’s an example:

ruby -e "puts 123 * 2"

This is great to quickly test out something without having to open IRB.

Online REPLs

What if you don’t have access to installing Ruby right now?

Or maybe you want to test something in a different environment than your local computer.

Or share code for demonstration purposes or asking for feedback.

That’s what online REPLs are great for.

One I like is https://repl.it/languages/ruby.

Summary

You’ve learned about REPLs in Ruby! A REPL allows you to quickly test code without having to write it to a file.

Now it’s practice time 🙂