liveBook · Manning

Irb

As explained in The Well-Grounded Rubyist, Fourth Edition

An interactive Ruby interpreter evaluates Ruby input entered incrementally in a session rather than processing a source file.

irb is Ruby’s interactive interpreter. Instead of processing a source file, it evaluates Ruby you enter during a session and prints the resulting value. Start it with `irb` at the command line; use `irb --simple-prompt` for a shorter prompt. Type `exit` to leave. section 1.4.2 section 1.1.1

Try `100 * 9 / 5 + 32`: irb responds with `=> 212`. No `print` or `puts` is needed. Variables persist for later entries in the session: enter `days = 365`, then use `days` in another expression. irb displays `365` for the assignment too, because an assignment expression has the value of its right-hand side. section 1.4.2

>> days = 365
=> 365
>> hours = 24
=> 24
>> minutes = 60
=> 60
>> days * hours * minutes
=> 525600

The session shows assignment results followed by a calculation using the assigned variables. section 1.4.2

Distinguish an expression’s displayed value from output produced by a method. Entering `puts "Hello"` prints `Hello`; irb then displays `=> nil`, the expression’s result. If you start an incomplete expression, irb changes its prompt and waits for you to finish it, even on the next line. section 1.4.2 section 1.1.2

A few session aids make experimentation easier:

  • Press Tab to cycle through automatic completions; method entries include valid signatures. Use Option+D, or Ctrl+D on Windows, to read method documentation. section 1.4.2
  • Press the up arrow to recall a command, or Ctrl+R to reverse-search command history. section 1.4.2
  • Preload a library with `-r`: `irb --simple-prompt -r rbconfig` makes installation configuration available for queries such as `RbConfig::CONFIG["bindir"]`. section 1.2

Use irb for small tests and questions about Ruby behavior; use the `ruby` interpreter when running a saved program file. An open session lets you try another expression without creating a file. section 1.4.2 section 1.1.1

How The Well-Grounded Rubyist, Fourth Edition covers it

How deep it goes
Explains startup, prompts, expression values, multiline input, session aids and library preloading, without examining interpreter internals.
Examples
Demonstrates arithmetic, a minutes-per-year calculation, variable assignments, `puts` output and an `rbconfig` directory query.
What it assumes
Assumes access to an installed Ruby command line while inviting experimentation with basic syntax rather than requiring it memorized.
What you can do after
Operate/configure it to evaluate snippets, navigate session history, use completion and preload a library.
How it connects
Connects interactive evaluation to assignment expressions, method return values, multiline expressions and Ruby installation configuration.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Well-Grounded Rubyist, Fourth Edition. The section links point to the parts of the book it draws on.