0
0
RubyHow-ToBeginner · 3 min read

How to Use IRB in Ruby: Interactive Ruby Shell Guide

Use irb by typing it in your terminal to start the Interactive Ruby Shell, where you can enter Ruby code line by line and see immediate results. It helps you test and experiment with Ruby code without creating files.
📐

Syntax

To start IRB, simply type irb in your terminal and press Enter. This opens an interactive prompt where you can type Ruby commands. To exit, type exit or press Ctrl+D.

Inside IRB, you can write any Ruby code and see the result immediately.

bash
irb
💻

Example

This example shows how to use IRB to add two numbers and print the result immediately.

ruby
2 + 3
# => 5
puts "Hello, IRB!"
# => Hello, IRB!
# => nil
Output
5 Hello, IRB!
⚠️

Common Pitfalls

One common mistake is trying to run IRB commands outside the IRB prompt or forgetting to start IRB first. Also, remember that IRB does not save your code automatically; you must copy it if you want to keep it.

Another pitfall is expecting multi-line code to run without proper syntax. Use do...end or proper indentation for multi-line blocks.

ruby
wrong:
puts "Hello"
puts "World"

right:
puts "Hello"
puts "World"
📊

Quick Reference

CommandDescription
irbStart the Interactive Ruby Shell
exitExit IRB
Ctrl+DShortcut to exit IRB
helpShow IRB help commands
puts 'text'Print text to the screen

Key Takeaways

Start IRB by typing irb in your terminal to run Ruby interactively.
Type Ruby code line by line and see immediate results inside IRB.
Exit IRB with exit or Ctrl+D when done.
IRB does not save your code; copy important snippets before exiting.
Use proper syntax for multi-line code blocks inside IRB.