irb: the interactive shell

Start irb

irb is the interactive Ruby shell: a prompt that runs each line the moment you press Enter. Open it with one command and you will see a prompt waiting for input.

Read more about interactive mode on the Ruby hub page, then come back and type along.

Open the interactive shell

irb

irb(main):001>

Try expressions

Inside irb every line is a small program. Type an expression, press Enter, and irb prints the result right away. The comment after each line below shows what irb prints back.

2 + 2 # => 4 "hi" * 3 # => "hihihi" name = "Ada" # => "Ada" puts "Hello, #{name}" # prints Hello, Ada

Nothing is saved to a file here, so irb is perfect for quick checks. When an idea works in irb, copy it into a script file later.

Help and tab completion

irb completes code for you. Type the first letters of a word such as puts and press Tab twice: irb shows or finishes the matching names.

The launcher also documents itself. Run it with the help flag to list the available options:

List irb command line options

irb --help

Exit

When you are done experimenting, leave the shell and return to the normal terminal. Either type the exit command or press Ctrl+D on an empty line.

Leave the interactive shell

exit

Next: Your first Ruby script

Article author: Arthur Isaev