just some thoughts about software and other things I like

Build a Simple Ruby REPL

REPL stands for read–eval–print loop, sometimes called an interactive environment. Many other languages and interpreters have a similar mechanism, like IRB with Ruby for example.

Find the code for this post on on Github.

Other nerds might call it an interactive toplevel or a language shell… but they should just knock it off, because that sounds fucking annoying.

Why would I want one?

Everyone has their go-to tools, especially in your regular system shell. Even though most developers have access to powerful tools like Rake, and Thor, you'll still often find yourself piping together sed, grep, and awk commands to do quick tasks.

But for more involved tasks that I find myself doing more often I'll build a rake task. Sometimes these things just don't cut it and you need to open up your system in a more controlled way.

I like to build out my toolchain for each project or system I work on. Often times that means making a command line interface to my application, this is the ideal case for a REPL.


I'm going to break it out in to three parts, first a method to handle the input.

When we handle the input, we would probably run some methods deep in our application or build a mini DSL for your methods you'd want to run.

def handle_input(input)
  result = eval(input)
  puts(" => #{result}")

This is a lambda that runs the content of the first block after the input is chomped.

repl = -> prompt do
  print prompt

After evaling and returning, fire up the prompt lambda again, this loops after every input and exits with exit or a HUP.

loop do
  repl[">> "]

Again, the project is on Github, fork it, let's make it more simple! Also this is going to be a part of a poker game that we'll be building over the next couple weeks.