How to Enter Numbers in Interactive Ruby - dummies

How to Enter Numbers in Interactive Ruby

By Christopher Haupt

To get ready to enter some numbers in Interactive Ruby, locate your terminal program (on the Mac) or the console with Ruby shortcut (on Windows). When that’s running, everything else is the same.

The term terminal means the terminal or console program, whether on Mac or Windows. Interactive Ruby is known as IRB or irb.

At the command prompt, enter the irb command and press Return (Mac) or Enter (Windows):

$ irb


C:/> irb

if on Windows.

You now see the irb prompt:

$ irb
2.2.2 :001 >

Depending on your version of Ruby, the prompt may appear slightly different. In this example, it shows the current version of Ruby (2.2.2) and the current number of commands typed (when you first start IRB, you start on command 001).

When you’ve started IRB, Ruby is waiting for something to do. Type the number 1 and press Return (Enter).

2.2.2 :001> 1
=> 1
2.2.2 :002 >

What did Ruby do? It showed you that it was listening by printing out number 1 again. The => prompt is Ruby’s signal that it’s displaying some kind of result. After it’s done, Ruby shows you a new prompt and waits for your next command.

Try entering a few more numbers, and you’ll see Ruby keeps echoing your typing. Okay, this gets boring quickly, so move on to something else.

Here’s how you leave IRB. The easiest way is to type exit and press Return (Enter). Alternatively, you can press Ctrl+D. You pop right out of IRB back to your terminal’s prompt.