irb beyond the prompt
A session with a prompt
irb is the interactive Ruby shell from lesson 1.3. It prints the result of every expression, which makes it the place to try one line before putting it in a file. The sessions below use --simple-prompt so the prompt is one character and the transcript is readable:
irb --simple-prompt
Switch to inspect mode.
>> 2 + 2
=> 4
>> exit
The last result is _
Every result is stored in _, so a long expression does not have to be retyped to be used in the next line. It is also the quickest way to check what a method returned without assigning it:
printf '1 + 1\n_\nexit\n' | irb --simple-prompt
Switch to inspect mode.
>> 1 + 1
=> 2
>> _
=> 2
>> exit
pp for a readable dump
A long nested object stays one long line under inspect. pp breaks it into indented lines when it does not fit, which is what you want for a hash of hashes or a JSON response:
printf 'pp({a: 1, b: [2, 3]})\nexit\n' | irb --simple-prompt
Switch to inspect mode.
>> pp({a: 1, b: [2, 3]})
{a: 1, b: [2, 3]}
=> {a: 1, b: [2, 3]}
>> exit
pp is a method from the standard library, so it works in a script too: require "pp" and call it instead of puts when a structure matters more than a sentence.
show_source
The next two sessions load a helper file, so write it first as lib.rb in the folder you start irb from:
def greet(name) "Hello, #{name}" end
irb can show the source of a method, including methods from gems, which saves a trip to the documentation when the question is "what does this do, exactly?":
printf 'show_source greet\nexit\n' | irb --simple-prompt -r ./lib.rb
Switch to inspect mode. >> show_source greet From: /tmp/b3/lib.rb:1 def greet(name) "Hello, #{name}" end >> exit
Load a file into the session
-r requires a file before the prompt appears, so a helper you are writing is available from the first line. Change the file, run load "./lib.rb" at the prompt, and the method is redefined without leaving the session.
printf 'greet("Ada")\nexit\n' | irb --simple-prompt -r ./lib.rb
Switch to inspect mode.
>> greet("Ada")
=> "Hello, Ada"
>> exit
In a real terminal the prompt keeps a history: the up arrow recalls the last lines, and Ctrl-R searches it. The piped sessions above are only a way to put the transcript on the page.
What to watch for
irb keeps state. A variable defined ten lines ago is still there, which
is convenient and also the reason a snippet that works in irb can fail in
a script: the script does not have the earlier lines. When a result
depends on something you typed before, put it in the file.
irb -r ./lib.rb works from the folder you start it
in. A require of a gem works anywhere; a
require_relative needs the file next to the one
that asks for it, which is the next lesson.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
Which option makes the irb prompt one character wide?
Answer
--simple-prompt
The sessions on the page use it so the transcript stays readable.
-
Which variable holds the last result in irb?
Answer
_
A bare underscore is the last value, as in the transcript
>> _followed by=> 2. -
What does the transcript
printf '1 + 1\n_\nexit\n' | irb --simple-promptend with?Answer
=> 2, then => 2 for the underscore
The addition gives 2, and the underscore repeats the same value.
-
What is pp for?
Answer
Printing a value in a pretty, indented form
pp is a printing method that breaks a long structure into indented lines.
-
Which irb command shows the source of a method?
Answer
show_source greet
show_source prints the file and the body of the method, also for a gem method.
-
What does
irb -r ./lib.rbdo?Answer
Requires the file before the prompt appears
The -r option names a file to require at startup, so its methods are ready from the first line.
-
Which command reloads an edited helper file inside a running session?
Answer
load "./lib.rb"
load reads and runs the file again, which redefines the method; require does nothing when the file is already loaded.
-
Why can a snippet that works in irb fail in a script?
Answer
Because irb keeps state the script does not have
A variable or method defined earlier in the session is still there, while a new script starts empty.
-
What does this program print?
def greet(name) "Hello, #{name}" end puts greet("Ada")Answer
Hello, Ada
The method returns the interpolated string, and puts prints it.
-
Which key searches the irb history in a real terminal?
Answer
Ctrl-R
Ctrl-R searches the lines typed before; the up arrow only recalls them one by one.
Article author: Arthur Isaev