why's (poignant) guide to ruby

Floating Little Leaves of Code

by _why the lucky stiff  ·  CC BY-SA 2.5
Original DomainRuby ProgrammingClassic 2003–2009
Chapter 4 illustration — the elfThe Elf — Chapter 4 character in Why's Poignant Guide

Every expression in Ruby evaluates to something. Even an if statement returns a value. Even a method definition returns a symbol. This is not a quirk — it is a design principle. Everything is an expression. Everything can be assigned to a variable.

result = if 1 < 2
  "math is intact"
else
  "we have a problem"
end

puts result  # => "math is intact"

The if/unless/while/until Family

if condition
  # runs if true
elsif other_condition
  # runs if first is false and this is true
else
  # runs if all above are false
end

unless condition   # same as: if !condition
  # runs if false
end

while condition
  # loops while true
end

until condition    # same as: while !condition
  # loops until true
end
The Elf contemplating Ruby conditionals

case/when

feeling = :hungry

case feeling
when :happy
  puts "dancing!"
when :hungry
  puts "eating chunky bacon"
when :tired
  puts "sleeping"
else
  puts "staring at the wall"
end

Useful Array Methods

nums = [3, 1, 4, 1, 5, 9, 2, 6]

nums.sort              # => [1, 1, 2, 3, 4, 5, 6, 9]
nums.sort.reverse      # => [9, 6, 5, 4, 3, 2, 1, 1]
nums.uniq              # => [3, 1, 4, 5, 9, 2, 6]
nums.min               # => 1
nums.max               # => 9
nums.sum               # => 31

nums.map    { |n| n * 2   }  # => [6, 2, 8, 2, 10, 18, 4, 12]
nums.select { |n| n > 4   }  # => [5, 9, 6]
nums.reject { |n| n > 4   }  # => [3, 1, 4, 1, 2]
nums.reduce { |sum, n| sum + n }  # => 31

nums.each_with_index do |n, i|
  puts "#{i}: #{n}"
end

Ranges

(1..5).to_a    # => [1, 2, 3, 4, 5]   (inclusive)
(1...5).to_a   # => [1, 2, 3, 4]     (exclusive end)
('a'..'e').to_a  # => ["a", "b", "c", "d", "e"]

(1..100).include?(42)  # => true
(1..100).min           # => 1
(1..100).max           # => 100

(1..10).step(2) { |n| print "#{n} " }  # => 1 3 5 7 9
The Elf and the Ruby Range

Hashes in Depth

scores = {}
scores['alice'] = 94
scores['bob']   = 87
scores['carol'] = 91

scores.each { |name, score| puts "#{name}: #{score}" }
scores.sort_by { |_name, score| -score }
# => [['alice', 94], ['carol', 91], ['bob', 87]]

scores.any?  { |_n, s| s > 90 }   # => true
scores.all?  { |_n, s| s > 80 }   # => true
scores.count { |_n, s| s >= 90 }  # => 2

String Formatting

name  = "Trady Blix"
score = 94.567

"%-15s %6.2f" % [name, score]  # => "Trady Blix       94.57"

# Also:
name.center(20, '-')   # => "-----Trady Blix-----"
name.ljust(20, '.')    # => "Trady Blix.........."
name.rjust(20)         # => "          Trady Blix"

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