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
