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.

  1. What does this program print? age = 20 if age >= 18 puts "adult" else puts "minor" end

    Answer

    adult

    The condition 20 >= 18 is true, so the if branch runs and else is skipped.

  2. What does this program print? age = 15 if age >= 18 puts "adult" else puts "minor" end

    Answer

    minor

    The condition is false, so else runs.

  3. What does puts "guest" unless logged_in do when logged_in is false?

    Answer

    Prints guest

    unless runs its statement when the condition is falsy, which false is.

  4. What does the same line do when logged_in is true?

    Answer

    Prints nothing

    A true condition turns the unless statement off.

  5. 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.

  6. What does this program print? score = 85 if score >= 90 puts "excellent" elsif score >= 70 puts "good" else puts "needs work" end

    Answer

    good

    The first branch is false, the elsif branch is true, and the rest is skipped.

  7. 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.

  8. What does this program print? role = :guest case role when :admin puts "full access" when :guest puts "read only" else puts "unknown" end

    Answer

    read only

    The second when matches the symbol, so its branch runs.

  9. 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.

  10. What does this program print? value = 0 if value puts "truthy" else puts "falsy" end

    Answer

    truthy

    Zero is truthy in Ruby, so the if branch runs.

Article author: Arthur Isaev