REPL
Explore the Ruby REPL environment to interactively execute code, understand its Read-Evaluate-Print-Loop process, and learn how to run Ruby programs from files. This lesson helps you write simple scripts using puts and gets, and introduces useful tools like irb and Pry for coding practice.
We'll cover the following...
Hello, I’m your REPL
In the case of the 1 + 1 program from the previous chapter, the interpreter performs two actions: read ® and evaluate (E). We used no “print” action (puts in our case), so no result was displayed on the screen. In other words, to see the results we need to perform these three actions:
- Read (R)
- Evaluate (E)
- Print (P)
It would also be nice if we could avoid running the ruby command from the terminal every time, so that we can execute programs in a constant loop (L). It turns out that this functionality is already there! The REPL acronym stands for Read Evaluate Print Loop.
It’s very similar to the Ruby interpreter, but differs in that it accepts the “Enter” key as the end of our program. Instead of exiting on Ctrl+D, signifying end of input, it just starts reading the input again. REPL is a well-known acronym, which is not tied to the Ruby language infrastructure. Other languages have their own REPLs too. Ruby’s REPL is called irb. We can type ...