Ruby Logo

Interactive Ruby (IRB)

Master the Ruby interactive shell for testing and learning.

Home Ruby Interactive Ruby (IRB)

Interactive Ruby (IRB)

Master the Interactive Ruby shell - your playground for experimenting with Ruby code, testing ideas, and learning new concepts.

What is IRB?

IRB (Interactive Ruby): A REPL (Read-Eval-Print Loop) that lets you execute Ruby code interactively, line by line.

Why Use IRB?

  • Quick Testing: Test code snippets without creating files
  • Learning Tool: Experiment with Ruby features and methods
  • Debugging: Inspect variables and test solutions
  • Prototyping: Build and test ideas quickly
  • Documentation: Try examples from Ruby docs

Starting IRB

Basic IRB Commands

Starting IRB
# Open your terminal and type irb
irb

# You'll see a prompt like this:
irb(main):001:0>
IRB Prompt Explanation
irb(main):001:0>
│ │ │ │
│ │ │ └─ Indent level
│ │ └───── Line number
│ └────────── Context (main)
└────────────── IRB prompt

Basic IRB Usage

Try These Examples

Simple Math
irb(main):001:0> 2 + 3
=> 5
irb(main):002:0> 10 * 5
=> 50
irb(main):003:0> 100 / 4
=> 25
Working with Strings
irb(main):004:0> "Hello, " + "World!"
=> "Hello, World!"
irb(main):005:0> name = "Ruby"
=> "Ruby"
irb(main):006:0> "Hello, #{name}!"
=> "Hello, Ruby!"
Arrays and Methods
irb(main):007:0> numbers = [1, 2, 3, 4, 5]
=> [1, 2, 3, 4, 5]
irb(main):008:0> numbers.length
=> 5
irb(main):009:0> numbers.reverse
=> [5, 4, 3, 2, 1]

Essential IRB Commands

Navigation Commands
  • exit - Quit IRB
  • quit - Quit IRB
  • help - Show help
  • clear - Clear screen
History Commands
  • history - Show command history
  • _ - Last result
  • _2 - Second to last result
  • ↑/↓ - Navigate history

Advanced IRB Features

Multi-line Input

Defining Methods
irb(main):010:0> def greet(name)
irb(main):011:1> "Hello, #{name}!"
irb(main):012:1> end
=> :greet
irb(main):013:0> greet("World")
=> "Hello, World!"
Loading Files
# Load a Ruby file into IRB
irb(main):014:0> load "hello.rb"
=> true

# Require a file (only loads once)
irb(main):015:0> require "./hello.rb"
=> true

Printing in IRB

Use puts to print friendly output. Use p to see the “raw” value (useful when learning).

irb(main):001:0> puts "Hello"
Hello
=> nil

irb(main):002:0> p "Hello"
"Hello"
=> "Hello"

irb(main):003:0> p [1,2,3]
[1, 2, 3]
=> [1, 2, 3]

IRB Best Practices

Tips for Effective IRB Usage

  • Use for experimentation: Test code snippets before writing full programs
  • Learn method signatures: Try methods to understand their parameters
  • Debug variables: Inspect object states and values
  • Practice regularly: Use IRB daily to reinforce learning
  • Explore documentation: Try examples from Ruby docs in IRB

Common IRB Mistakes

  • Forgetting to close quotes: IRB will show continuation prompts
  • Not using proper indentation: Multi-line blocks need correct indentation
  • Confusing = and ==: Use = for assignment, == for comparison
  • Not checking return values: Always look at the => output

Practice Exercises

  1. Print your name using puts.
  2. Store a number in a variable and add 5 to it. Print the result.
  3. Create an array of three colors and print it with p.

Mini Quiz

  • What does puts do in IRB?
  • What’s one difference between puts and p?
  • How do you exit IRB?

What's Next?

Continue Your Ruby Journey

  1. Practice in IRB: Experiment with different Ruby features daily
  2. Learn Variables: Understand different types of variables in Ruby
  3. Explore Data Types: Master strings, numbers, arrays, and hashes
  4. Study Methods: Learn how to define and use methods
  5. Read Documentation: Use IRB to test examples from Ruby docs

Quick Navigation

Related Topics

Video Tutorial

Watch and learn interactive ruby (irb)

Pro Tip: After reading through the content above, watch this video to reinforce your understanding and see the concepts in action!