Lesson Weekend

We'll start by using a REPL (Read - Evaluate - Print - Loop) program called IRB to write Ruby. IRB stands for Interactive RuBy and it runs in the terminal. We can use IRB to experiment with Ruby code and see immediate results.

Accessing IRB


Launch the command line and enter:

$ irb

Alternatively on Windows, you can also try opening Rails Installer > Interactive Ruby from the Ruby section of the Start Menu.

How IRB Works


This is how a Read-Eval-Print-Loop tool works:

  1. You enter a line of code which will be read when you hit Enter.
  2. The REPL evaluates the code to figure out what it does, and executes it.
  3. The REPL prints the result to the screen for you to see.
  4. The REPL loops back to step 1 and waits for your next entry.

In IRB, you'll have a prompt that looks something like irb(main):001:0>. We'll abbreviate this prompt to: > in our lessons. You can follow along in your own irb shell by typing everything after the >. When you press Enter at the end of a line, IRB will evaluate the code that you wrote and give you back a result on the next line, which will start with the symbol =>.

Try It!


Try it out by entering a string at the prompt:

> "hello"
=> "hello"

IRB reads and evaluates our Ruby string and the result was the string itself.

We can also enter numbers for a math operation:

> 4 + 3
=> 7

IRB evaluated our Ruby math expression and the result was the sum: 7.

When you want to exit IRB, simply type exit and then Enter.

One thing you should be careful of in IRB is forgetting to close out an expression. For example, if you type:

"hello.length()

Nothing will happen. That's because there are no closing quotes after "hello, which is a string. IRB is waiting for the closing quotes. If you accidentally do this, you can just type " and press Enter so that Ruby knows to move on. The same thing can happen if you forget to close your parentheses for arguments. You can also press Ctrl + C (even on a Mac it's Ctrl, not Command), and IRB will cancel anything you’ve typed in and go to a new line.

Interactive RuBy is a REPL. This is how a Read-Eval-Print-Loop tool works:

  1. You enter a line of code which will be read when you hit Enter.
  2. The REPL evaluates the code to figure out what it does, and executes it.
  3. The REPL prints the result to the screen for you to see.
  4. The REPL loops back to step 1 and waits for your next entry.

Type irb in the console to open IRB. When you want to exit IRB, simply type exit and then Enter.

Lesson 1 of 10
Last updated August 7, 2022