why's (poignant) guide to ruby

Expansion Pak 1: The Tiger’s Vest (with a Basic Introduction to IRB)

by _why the lucky stiff  ·  CC BY-SA 2.5
Original DomainRuby ProgrammingClassic 2003–2009
The Tiger's Vest — Expansion Pak illustration

IRB stands for Interactive Ruby. It is the Ruby REPL — Read-Eval-Print Loop. You type a Ruby expression, it evaluates it, prints the result, and waits for the next one. IRB is the best way to experiment and explore.

Start it from your terminal:

$ irb
irb(main):001:0>

Using IRB

irb(main):001:0> 2 + 2
=> 4
irb(main):002:0> "hello".upcase
=> "HELLO"
irb(main):003:0> [1, 2, 3].map { |n| n ** 2 }
=> [1, 4, 9]

Exploring Objects in IRB

One of IRB’s most useful features is letting you discover what methods an object has:

irb(main):001:0> "hello".methods.sort
=> [:%, :*, :+, :+@, :-@, :<<, :<=>, ... :upcase, :upcaseI, ...]

irb(main):002:0> "hello".methods.grep(/case/)
=> [:swapcase, :swapcase!, :upcase, :upcaseI, :downcase, :downcaseI, :capitalize, :capitalizeI]

irb(main):003:0> 42.class
=> Integer

irb(main):004:0> Integer.superclass
=> Numeric

Multi-line Input

irb(main):001:0> [1, 2, 3].map do |n|
irb(main):002:1*   n * 10
irb(main):003:1> end
=> [10, 20, 30]

Loading Files

irb(main):001:0> load 'my_program.rb'  # loads and runs the file
irb(main):002:0> require 'json'         # loads a library

The Tiger’s Vest

That’s the Expansion Pak. IRB is your laboratory. Use it constantly. Try things. Break things. See what Ruby does when you push it into unexpected situations. That is how you learn the edges of the language, and the edges are where the interesting things live.

The tiger’s vest is yours now. Wear it well.

The Ruby community continues at ruby-lang.org. The Poignant Guide’s cultural legacy continues every August 19th on Whyday.

Why's (Poignant) Guide to Ruby is released under the Creative Commons Attribution-ShareAlike 2.5 license. Written by _why the lucky stiff, originally published 2003–2009. Preserved at its original domain. Images © _why the lucky stiff, CC BY-SA 2.5.

→ AI Detector Benchmark & Research Hub