A place to try Ruby one expression at a time
IRB stands for Interactive Ruby. It is a REPL: a read-eval-print loop. It reads what you type, evaluates complete Ruby code, shows the resulting value, then prompts for more input. It is useful for a quick calculation, checking a method or trying an idea without first saving a program. IRB is included as a default gem in ordinary Ruby installations.
IRB is still Ruby, so the same language rules apply. The prompt style and display format can differ by installed IRB version or settings. The examples below use => to label a value that IRB displays; you type only the Ruby expression after the prompt.
Start and leave an IRB session
Open a system terminal after installing Ruby and type irb. When a prompt appears, type 2 + 3 and press Enter. IRB shows the resulting number 5. Type exit to leave the session and return to the operating-system shell. If irb is not found, check that the selected Ruby installation and its default gems are available, and reopen the terminal after installing.
Do not type ruby hello.rb at the IRB prompt to run a saved file: that is a shell command. Leave IRB first, then run the file from your terminal.
Terminal — start IRB
irbInside IRB — enter these lines one at a time
2 + 3
exitUnderstand the result IRB displays
An expression produces a value. For example, 2 + 3 evaluates to 5, and "hello".upcase evaluates to "HELLO". The dot calls the upcase method on a String, which returns a new uppercase String. IRB shows these values even when you did not call a printing method.
This displayed result is different from the output of puts. The transcript below illustrates the difference. IRB shows a String result for a bare String expression. puts writes the greeting as program output, then IRB displays nil because puts returns nil. A saved Ruby script does not automatically display the value of every expression; use puts when you want visible output.
Illustrative IRB transcript — prompt format may vary
> "Hello, Codectionary!"
=> "Hello, Codectionary!"
> puts "Hello, Codectionary!"
Hello, Codectionary!
=> nilKeep a value for later expressions
Type name = "Sam". The variable name now refers to that String for the rest of this IRB session. Type name.upcase to call a method on the stored String, then type "Hello, #{name}!" to interpolate its value into a new String. IRB displays the returned values; nothing is written by puts in these three expressions.
Try changing the assignment to name = "Alex" and run the interpolation again. Each expression sees the current variable value. Closing this IRB session does not automatically save those lines in a .rb file.
Inside IRB — type one expression at a time
name = "Sam"
name.upcase
"Hello, #{name}!"Experiment with a method
Define greet as shown below. The method accepts one argument and returns a String containing that argument. IRB may show a method-name symbol after the end line; that is the result of defining the method, not greeting output. Calling greet("Sam") then displays the returned String. Calling puts greet("Sam") prints the greeting and returns nil.
Methods defined in this session remain available until you exit it. If you want to keep your work, write the definition in a .rb file and run that file from a terminal.
Inside IRB — define and call a method
def greet(name)
"Hello, #{name}!"
end
greet("Sam")
puts greet("Sam")When to use IRB or a saved file
Use IRB when you want immediate feedback on one expression or a short experiment. Use a .rb file when the code should be saved, reviewed, rerun or shared. A file runs from its beginning each time you execute ruby filename.rb; an IRB session keeps state as you enter expressions, which can make an experiment depend on earlier input.
If an IRB prompt waits rather than showing a result, your input may be incomplete, such as an unclosed String or a def without end. Complete the expression or leave the session and start again. Do not confuse an IRB result such as => nil with an error: it simply reports a returned value.
Syntax
irbExamples
An expression result versus printed output
Enter each expression inside IRB; the transcript labels IRB results with =>.
"Hello, Codectionary!"
puts "Hello, Codectionary!"- The first expression returns a String, which IRB displays as a result.
- The second expression calls puts, which prints a greeting as standard output.
- IRB then displays nil, the value returned by puts. A saved script would only show the printed greeting.
Try a String method
Enter these lines in one IRB session.
name = "Sam"
name.upcase
"Hello, #{name}!"- The variable stores a String for later expressions in this session.
- upcase returns "SAM" without changing the original name String.
- Interpolation returns "Hello, Sam!"; IRB displays it even without puts.
Best practices
- Type Ruby expressions at the IRB prompt and shell commands at the system terminal.
- Distinguish IRB’s displayed return values from output printed by your code.
- Move an experiment into a .rb file when you want to save and rerun it.
Runtime & versions
Ruby interpreter. Feature availability depends on the language and runtime version used by your project; consult the official reference below.
Ruby documentation