Control Flow
If and else
Ruby modifiers and blocks are both common.
score = 82
if score >= 70
puts "passed"
else
puts "retry"
end
puts "ok" if score >= 70
Case expressions
case compares values cleanly.
day = "sat"
kind = case day
when "sat", "sun" then "weekend"
else "weekday"
end
puts kind
While and times
Use iterators when you can; while remains available.
3.times { |i| puts i }
n = 3
while n > 0
puts n
n -= 1
end
Flow tips
- Everything has a value—including if and case
- Prefer
eachover manual index loops nextandbreakcontrol iteration- Nil is falsy; only
falseandnilare falsy
Comments
One comment per signed-in account. Comments are saved with this page’s URL.