Conditions
if and else
The if statement runs a block only when its condition holds, and else covers everything that fails the check. Close the branch with the end keyword.
age = 20 if age >= 18 puts "adult" else puts "minor" end
ruby adult.rb
adult
unless modifier
unless is if turned upside down: the line runs only when the condition is falsy. As a one-line modifier at the end, it reads like plain advice and avoids a negation mark.
logged_in = false puts "guest" unless logged_in
ruby guest.rb
guest
elsif chains
When several outcomes exclude each other, chain elsif branches between if and else. Only the first block with a true condition runs, so order the checks from the strictest to the loosest one.
Note the spelling: Ruby writes elsif with one e in the middle.
score = 85 if score >= 90 puts "excellent" elsif score >= 70 puts "good" else puts "needs work" end
ruby grade.rb
good
case-when
case compares one value against a list of candidates and runs the first match. Each when names a candidate, and else catches anything unmatched at the bottom.
role = :admin case role when :admin puts "full access" when :guest puts "read only" else puts "unknown" end
ruby role.rb
full access
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does this program print?
age = 20 if age >= 18 puts "adult" else puts "minor" endAnswer
adult
The condition 20 >= 18 is true, so the if branch runs and else is skipped.
-
What does this program print?
age = 15 if age >= 18 puts "adult" else puts "minor" endAnswer
minor
The condition is false, so else runs.
-
What does
puts "guest" unless logged_indo when logged_in is false?Answer
Prints guest
unless runs its statement when the condition is falsy, which false is.
-
What does the same line do when logged_in is true?
Answer
Prints nothing
A true condition turns the unless statement off.
-
How does Ruby spell the middle branch of an if chain?
Answer
elsif
Ruby writes elsif with one e in the middle, unlike most other languages.
-
What does this program print?
score = 85 if score >= 90 puts "excellent" elsif score >= 70 puts "good" else puts "needs work" endAnswer
good
The first branch is false, the elsif branch is true, and the rest is skipped.
-
Why does the lesson say to order the checks from strictest to loosest?
Answer
Only the first true branch runs, so a loose check above would win
The chain stops at the first true condition, so a broad check placed early hides the narrow ones.
-
What does this program print?
role = :guest case role when :admin puts "full access" when :guest puts "read only" else puts "unknown" endAnswer
read only
The second when matches the symbol, so its branch runs.
-
What does the else of a case do?
Answer
Catches every value that matched no when
It is the fallback branch, like the else of an if.
-
What does this program print?
value = 0 if value puts "truthy" else puts "falsy" endAnswer
truthy
Zero is truthy in Ruby, so the if branch runs.
Article author: Arthur Isaev