← Back to Contents

Chapter 1: Environment Setup

Install Ruby 3.3, configure development environment

1. Installing Ruby 3.3

Ruby 3.3 is the latest stable release with significant performance improvements and YJIT enabled by default. Choose the installation method for your platform below.

macOS (Homebrew + rbenv)

On macOS, rbenv is recommended for managing multiple Ruby versions:

# Install rbenv and ruby-build via Homebrew
brew install rbenv ruby-build

# Add rbenv to your shell (add to ~/.zshrc or ~/.bashrc)
eval "$(rbenv init -)"

# Install Ruby 3.3
rbenv install 3.3.0
rbenv global 3.3.0

# Verify installation
ruby -v

Tip: macOS ships with a system Ruby, but it's outdated and locked down. Always use a version manager like rbenv or asdf to install a user-level Ruby.

Ubuntu / Debian

On Ubuntu, use rbenv installed via git, or use the system package manager for a quick setup:

# Option 1: rbenv (recommended)
sudo apt install -y git curl libssl-dev libreadline-dev zlib1g-dev
curl -fsSL https://github.com/rbenv/rbenv-installer/raw/HEAD/bin/rbenv-installer | bash

# Add to ~/.bashrc
echo 'eval "$(~/.rbenv/bin/rbenv init -)"' >> ~/.bashrc
source ~/.bashrc

# Install Ruby 3.3
rbenv install 3.3.0
rbenv global 3.3.0

# Option 2: System package (may be older)
sudo apt install ruby-full

# Verify
ruby -v

Windows

On Windows, the recommended approach is RubyInstaller:

# Download RubyInstaller from https://rubyinstaller.org/
# Choose "Ruby+Devkit 3.3.x (x64)" and run the installer

# Or use Scoop
scoop install ruby

# Or use Chocolatey
choco install ruby

# Verify
ruby -v

Verify Installation

Regardless of platform, you should see output like this:

$ ruby -v
ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [arm64-darwin23]

$ gem -v
3.5.3

πŸ”„ Comparison with other languages

Python uses python3 --version, Node.js uses node -v, Java uses java --version. Ruby follows the same pattern with ruby -v.

2. IRB Interactive Environment

Ruby ships with IRB (Interactive Ruby), a REPL for quickly testing code snippets. It's one of the fastest ways to explore the language.

$ irb
irb(main):001> puts "Hello, Ruby!"
Hello, Ruby!
=> nil
irb(main):002> 2 ** 10
=> 1024
irb(main):003> [1, 2, 3].map { |n| n * n }
=> [1, 4, 9]
irb(main):004> "hello".upcase.reverse
=> "OLLEH"
irb(main):005> exit

Tip: Ruby 3.3+ includes irb with autocomplete, syntax highlighting, and multi-line editing out of the box. You can also try pry (install with gem install pry) for an even more powerful REPL with debugging capabilities.

IRB is great for quick experiments:

# Check available methods on any object
"hello".methods.sort
# => [:!, :!=, :!~, :%, :*, :+, :+@, :-@, :<, :<<, ...]

# Quick type checking
42.class        # => Integer
3.14.class      # => Float
"text".class    # => String
[1,2].class     # => Array
{a: 1}.class    # => Hash

# Ruby's everything-is-an-object philosophy
5.even?         # => false
5.odd?          # => true
nil.nil?        # => true

πŸ”„ REPL comparison

Ruby irb — interactive Ruby shell with autocomplete
Python python3 — interactive Python interpreter
Node.js node — V8 REPL

3. Installing Bundler

Bundler is Ruby's dependency manager. It reads a Gemfile, resolves dependencies, and installs the exact versions your project needs. It ships with Ruby 3.3+ by default.

Install & Verify

# Bundler comes with Ruby 3.3+, but you can update it
gem install bundler

# Verify
bundler -v
# => Bundler version 2.5.x

Create a Gemfile

A Gemfile lists your project's dependencies, similar to package.json in Node.js:

# Gemfile
source "https://rubygems.org"

ruby "~> 3.3"

gem "httparty", "~> 0.21"
gem "nokogiri", "~> 1.16"

group :development, :test do
  gem "pry", "~> 0.14"
  gem "rspec", "~> 3.13"
end

Install Dependencies

# Install all gems defined in Gemfile
bundle install

# Or simply
bundle

# Add a new gem to Gemfile and install it
bundle add faraday

# Run a Ruby script within the bundle context
bundle exec ruby my_script.rb

Important: Gemfile.lock pins exact versions for reproducibility. Always commit it to version control. When deploying, use bundle install --deployment to install from the lock file.

πŸ”„ Package manager comparison

Language Tool Config File Lock File
Ruby Bundler Gemfile Gemfile.lock
Node.js npm package.json package-lock.json
Python pip requirements.txt
PHP Composer composer.json composer.lock
Java Maven pom.xml

4. Hello World

Let's write a slightly more complete Ruby script to get a feel for the basic syntax:

# hello.rb
name = "Ruby"
version = 3.3
features = ["YJIT", "Pattern Matching", "Ractor", "Fiber Scheduler"]

puts "Hello, #{name} #{version}!"

puts "Ruby #{version} highlights:"
features.each_with_index do |feature, index|
  puts "  #{index + 1}. #{feature}"
end

def greet(who)
  "Welcome to #{who}!"
end

puts greet("Ruby 3.3")

Save it and run from the terminal:

$ ruby hello.rb
Hello, Ruby 3.3!
Ruby 3.3 highlights:
  1. YJIT
  2. Pattern Matching
  3. Ractor
  4. Fiber Scheduler
Welcome to Ruby 3.3!

Syntax key points:

  • • No semicolons needed — line breaks end statements
  • • No variable type declarations — Ruby is dynamically typed
  • • String interpolation with #{expression} inside double quotes
  • puts prints with a newline, print without
  • • Methods implicitly return their last expression
  • • Blocks use do..end or { }

πŸ”„ Run command comparison

Ruby ruby hello.rb
Python python hello.py
Node.js node hello.js
PHP php hello.php

Ruby, like Python and PHP, is an interpreted language — no compilation step needed.

5. VS Code Configuration

VS Code is a popular choice for Ruby development. Install these extensions for a great experience:

🧠

Ruby LSP

Official language server by Shopify. Provides intelligent code completion, go-to-definition, inline diagnostics, and formatting.

Shopify.ruby-lsp
πŸ”

Solargraph

Alternative language server with YARD documentation support, type checking, and cross-reference navigation.

castwide.solargraph
πŸ›

Ruby Debug

Debug adapter for the debug gem (built into Ruby 3.1+). Supports breakpoints, stepping, and variable inspection.

KoichiSasada.vscode-rdbg

Install the gems that power these extensions:

gem install ruby-lsp solargraph debug

Recommended .vscode/settings.json for your project:

{
    "editor.formatOnSave": true,
    "[ruby]": {
        "editor.defaultFormatter": "Shopify.ruby-lsp",
        "editor.tabSize": 2
    },
    "rubyLsp.formatter": "auto"
}

Debugging: Ruby 3.1+ bundles the debug gem. You can set breakpoints in VS Code or insert binding.irb (or binding.pry with pry) directly in your code to drop into a REPL at that point.

6. Chapter Summary

πŸ› οΈ Install Ruby

Use rbenv on macOS/Linux, RubyInstaller on Windows. Verify with ruby -v. Always use a version manager rather than the system Ruby.

πŸ’» IRB REPL

Type irb to launch the interactive console. Perfect for quick experiments, method discovery, and learning. Ruby 3.3 includes autocomplete by default.

πŸ“¦ Bundler

Ruby's package manager. Define dependencies in Gemfile, run bundle install, and commit Gemfile.lock for reproducible builds.

πŸ“ Basic Syntax

No semicolons, dynamic typing, string interpolation with #{}, implicit return, everything is an object.

πŸš€ Hello World

Write a .rb file and run with ruby filename.rb. Use puts for output, def..end for methods, blocks for iteration.

πŸ’» VS Code

Install Ruby LSP (or Solargraph) for intellisense, Ruby Debug for breakpoint debugging. Set tab size to 2 spaces per Ruby convention.