Ruby IRB
Interactive Ruby shell for testing code
๐ป What is IRB?
IRB (Interactive Ruby) is a command-line tool for executing Ruby code interactively. It lets you test code snippets, experiment with methods, and debug programs in real-time without creating files. Perfect for learning and quick testing.
# Start IRB from terminal
$ irb
# Type Ruby code and see results instantly
irb(main):001:0> puts "Hello, IRB!"
Hello, IRB!
=> nil
Output:
Hello, IRB!
=> nil
Key IRB Features
Instant Feedback
See results immediately
2 + 2
=> 4
Explore Objects
Inspect methods and classes
"hello".methods
History
Access previous commands
โ arrow key
Testing
Try code before saving
def test; end
๐น Starting IRB
Launch IRB from your terminal to start an interactive Ruby session. You can type Ruby code and see results immediately, making it perfect for experimentation and learning.
# Start IRB
$ irb
# You'll see the IRB prompt
irb(main):001:0>
# Type Ruby code
irb(main):001:0> name = "Ruby"
=> "Ruby"
irb(main):002:0> puts "Hello, #{name}!"
Hello, Ruby!
=> nil
Output:
Hello, Ruby!
=> nil
๐น Basic IRB Operations
IRB evaluates Ruby expressions and displays their return values. You can perform calculations, create variables, call methods, and test any Ruby code interactively in the session.
# Math operations
irb(main):001:0> 10 + 5
=> 15
# String operations
irb(main):002:0> "Ruby".upcase
=> "RUBY"
# Arrays
irb(main):003:0> [1, 2, 3].sum
=> 6
# Variables
irb(main):004:0> x = 100
=> 100
irb(main):005:0> x * 2
=> 200
Output:
=> 15
=> "RUBY"
=> 6
=> 100
=> 200
๐น Defining Methods in IRB
You can define and test methods directly in IRB. Use multi-line input to create methods, then call them immediately to verify they work as expected.
# Define a method
irb(main):001:0> def greet(name)
irb(main):002:1> "Hello, #{name}!"
irb(main):003:1> end
=> :greet
# Call the method
irb(main):004:0> greet("Alice")
=> "Hello, Alice!"
# Define a method with logic
irb(main):005:0> def even?(num)
irb(main):006:1> num % 2 == 0
irb(main):007:1> end
=> :even?
irb(main):008:0> even?(4)
=> true
Output:
=> "Hello, Alice!"
=> true
๐น Useful IRB Commands
IRB provides special commands to enhance your interactive experience. These commands help you navigate, inspect objects, load files, and control your IRB session effectively.
Common Commands:
- exit or quit - Leave IRB
- help - Show help information
- load 'file.rb' - Load a Ruby file
- require 'gem' - Load a gem
- _ - Access last result
# Use last result
irb(main):001:0> 5 + 5
=> 10
irb(main):002:0> _ * 2
=> 20
# Load a file
irb(main):003:0> load 'my_script.rb'
=> true
# Exit IRB
irb(main):004:0> exit
Output:
=> 10
=> 20
=> true
๐น Exploring Objects
IRB is excellent for discovering what methods are available on objects. Use methods like .methods, .class, and .inspect to learn about Ruby objects and their capabilities.
# Check object class
irb(main):001:0> "hello".class
=> String
# List all methods
irb(main):002:0> "hello".methods
=> [:upcase, :downcase, :length, :reverse, ...]
# Check if method exists
irb(main):003:0> "hello".respond_to?(:upcase)
=> true
# Inspect object
irb(main):004:0> [1, 2, 3].inspect
=> "[1, 2, 3]"
Output:
=> String
=> [:upcase, :downcase, :length, ...]
=> true
=> "[1, 2, 3]"
๐น IRB Configuration
Customize IRB behavior by creating an .irbrc file in your home directory. This file runs automatically when IRB starts, allowing you to set preferences and load commonly used gems.
# ~/.irbrc file
require 'irb/completion' # Enable tab completion
# Custom prompt
IRB.conf[:PROMPT_MODE] = :SIMPLE
# Auto-indent
IRB.conf[:AUTO_INDENT] = true
# Load commonly used gems
require 'json'
require 'date'
puts "IRB configured and ready!"
Output:
IRB configured and ready!
>>