πŸ’Ž

A language tour

The Beauty of Ruby

Code that reads like poetry β€” expressive, elegant, and joyful to write.

scroll

01 β€” Expressiveness

Code that reads like English

Ruby was designed for programmer happiness. Matz wanted a language that felt natural β€” like writing a sentence, not a specification.

natural_language.rb
# Ruby reads like a wish list
5.times { puts "Hello, world!" }

# Asking real questions
age = 17
puts age.odd?     # β†’ true  (predicate methods end in ?)
puts age.between?(10, 20)  # β†’ true

# Unless is built-in
puts "You're a minor" unless age >= 18

# One-liner conditionals feel like prose
puts "Go to bed!" if Time.now.hour > 22

Method names end in ? when they return booleans β€” the language winks at you.


02 β€” Blocks & Iterators

Iteration without ceremony

Forget index variables and off-by-one errors. Ruby's blocks make looping a pleasure β€” and you can pass behaviour around like data.

"Blocks are chunks of code you can hand to a method and say: here, do this when you're ready."

β€” The Ruby philosophy
blocks.rb
numbers = [3, 1, 4, 1, 5, 9, 2, 6]

# Transform every element
squares = numbers.map { |n| n ** 2 }
# β†’ [9, 1, 16, 1, 25, 81, 4, 36]

# Filter with intent
evens = numbers.select(&:even?)
# β†’ [4, 2, 6]

# Chain it all β€” pipeline style
result = numbers
  .select { |n| n > 3 }
  .map    { |n| n * 10 }
  .sum
# β†’ 200

&:method_name converts a symbol to a block β€” the shortest syntax for "call this method on each element."


03 β€” Open Classes

Everything is alive

In Ruby, every class β€” even built-ins like Integer or String β€” can be reopened and extended. The language bends to your domain.

open_classes.rb
class Integer
  def factorial
    return 1 if self <= 1
    self * (self - 1).factorial
  end

  def seconds; self;         end
  def minutes; self * 60;   end
  def hours;   self * 3600; end
end

puts 5.factorial   # β†’ 120
puts 2.hours        # β†’ 7200

class String
  def palindrome?
    self == self.reverse
  end
end

puts "racecar".palindrome?  # β†’ true
puts "ruby".palindrome?     # β†’ false

You didn't "monkey-patch" β€” you taught the number what a factorial means. That's a different mindset.


04 β€” Symbols & Hashes

Data structures that delight

Ruby's hashes and symbols feel lightweight and intentional β€” no boilerplate, just the structure you need.

data.rb
# Modern hash syntax β€” clean and readable
car = {
  make:   "Porsche",
  model:  "911",
  year:   1973,
  color:  "Guards Red"
}

# Safe navigation β€” no more nil crashes
user = nil
puts user&.name  # β†’ nil  (not an error!)

# Destructuring with pattern matching (Ruby 3+)
case car
in { make: "Porsche", year: (1970..1979) => year }
  puts "Classic air-cooled from #{year}!"
end
# β†’ Classic air-cooled from 1973!

The &. safe navigation operator is called "lonely operator" β€” because it guards you from nil in the loneliest of times.


05 β€” Modules & Mixins

Composition over inheritance

Ruby avoids deep inheritance trees. Instead, modules let you compose behaviour cleanly β€” mix in exactly what you need.

mixins.rb
module Driveable
  def drive
    puts "#{self.class} is driving!"
  end
end

module Electric
  def charge
    puts "Plugging in… ⚑"
  end
end

class Car
  include Driveable
end

class ElectricCar < Car
  include Electric
end

tesla = ElectricCar.new
tesla.drive   # β†’ ElectricCar is driving!
tesla.charge  # β†’ Plugging in… ⚑

# Enumerable mixin β€” mix it in, get 50+ methods free
class Playlist
  include Enumerable

  def initialize(songs)
    @songs = songs
  end

  def each(&block)
    @songs.each(&block)
  end
end

pl = Playlist.new(["Bohemian Rhapsody", "Comfortably Numb", "Hotel California"])
puts pl.min    # β†’ Bohemian Rhapsody (alphabetically)
puts pl.sort.first

Including Enumerable and defining one each method unlocks map, sort, min, group_by, flat_map and 40+ more for free.


06 β€” The Whole Picture

More reasons to love it

πŸ”—

Method Chaining

Every method returns something useful β€” chains flow left-to-right like a pipeline.

🧬

Everything Is an Object

Even nil, true, and numbers are objects. No primitive types β€” it's objects all the way down.

🎭

Duck Typing

"If it walks like a duck…" β€” Ruby cares what an object can do, not what it is.

✨

String Interpolation

"Hello #{name}" β€” weave values into strings without concatenation noise.

πŸ› 

Rails & Ecosystem

Ruby on Rails showed the world what convention-over-configuration looks like in practice.

😊

Joy First

Matz's goal: "Ruby is designed to make programmers happy." That's still in the language DNA.