Ruby offers standard control-flow (if, , ) but with distinctive, expressive features: (), , the powerful , and a preference for over traditional loops. Control flow in Ruby reads naturally.
casewhiledo_x if conditionunlesscase/whenif age >= 18
puts "adult"
elsif age >= 13
puts "teen"
else
puts "child"
end
# MODIFIER form — condition at the END (reads like English, idiomatic for short cases)
puts "adult" if age >= 18
puts "minor" unless age >= 18 # `unless` = if not (more readable than `if !`)
# everything is an expression — if returns a value
grade = if score >= 90 then "A" else "B" end
Ruby's modifier form (puts "x" if condition) puts the condition at the end for concise, readable one-liners. unless (= "if not") reads more naturally than negation. And if is an expression that returns a value.
case status
when :active then "running"
when :stopped, :paused then "halted" # multiple values
when 1..10 then "small number" # ranges
when String then "it's a string" # types (uses ===)
else "unknown"
end
# case can also do structural pattern matching (Ruby 3)
case data
in { name: String => name, age: Integer } # pattern matching with destructuring
puts name
end
Ruby's case/when is powerful — it matches values, ranges, types, and (in Ruby 3) structural patterns, using === under the hood. Far more flexible than a basic switch.
while count < 10 do count += 1 end
until done do ... end # until = while not
# ✅ but Ruby PREFERS iteration with blocks over explicit loops:
5.times { |i| puts i } # instead of a for loop
[1, 2, 3].each { |n| puts n } # iterate a collection
(1..10).each { |n| ... } # iterate a range
1.upto(5) { |i| ... }
Ruby has while/until, but idiomatic Ruby prefers iteration with blocks (each, times, map) over explicit loops — more expressive and Ruby-like.
Understanding Ruby's control flow is fundamental everyday knowledge, and several distinctive features reflect Ruby's expressive, readable philosophy.
The modifier form (do_x if condition) and unless (= "if not") let you write concise, English-like conditionals that are idiomatic Ruby and appear constantly.
The fact that if is an expression (returning a value) enables elegant assignments.
Ruby's case/when is notably powerful — matching values, ranges, types, and (in Ruby 3) structural patterns via === — making it far more flexible than a basic switch and useful for clean multi-way branching.
Crucially, idiomatic Ruby prefers iteration with blocks (each, times, map, upto) over traditional for/while loops — this is a defining stylistic point: Ruby developers iterate collections and ranges with block-based methods rather than explicit loops, which is more expressive and Ruby-like.
Understanding these constructs and idioms — the modifier form and unless for readable conditionals, the powerful case/when, and especially the preference for block-based iteration over explicit loops — is important for writing and reading idiomatic Ruby.
Since control flow appears in every program, and since Ruby's expressive forms (modifiers, unless, powerful case, block iteration) embody its readability-focused philosophy, mastering them is essential, foundational knowledge for writing natural, idiomatic Ruby rather than translating loop-heavy patterns from other languages.