Challenge - 5 Problems
Ruby Control Flow Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Ruby if-else vs ternary operator
What is the output of this Ruby code snippet?
Ruby
x = 5 result = if x > 3 "big" else "small" end puts result
Attempts:
2 left
💡 Hint
Check the condition x > 3 and what the if-else returns.
✗ Incorrect
The if-else checks if x is greater than 3. Since 5 > 3 is true, it returns "big".
❓ Predict Output
intermediate2:00remaining
Output of Ruby case statement with when clauses
What will this Ruby code print?
Ruby
grade = 'B' case grade when 'A' puts 'Excellent' when 'B' puts 'Good' else puts 'Needs Improvement' end
Attempts:
2 left
💡 Hint
Look at the value of grade and which when clause matches.
✗ Incorrect
The case matches 'B' with the second when clause, so it prints 'Good'.
❓ Predict Output
advanced2:00remaining
Output of Ruby while vs until loops
What is the output of this Ruby code?
Ruby
i = 0 while i < 3 print i i += 1 end j = 3 until j == 0 print j j -= 1 end
Attempts:
2 left
💡 Hint
Check how while and until loops run and what they print.
✗ Incorrect
The while loop prints 0,1,2 and the until loop prints 3,2,1 concatenated as '012321'.
❓ Predict Output
advanced2:00remaining
Output of Ruby modifier if and unless
What will this Ruby code output?
Ruby
x = 10 puts 'High' if x > 5 puts 'Low' unless x > 5
Attempts:
2 left
💡 Hint
Understand how modifier if and unless work.
✗ Incorrect
Since x is 10, x > 5 is true, so 'High' prints. The unless condition is false, so 'Low' does not print.
🧠 Conceptual
expert3:00remaining
Reason for multiple control flow styles in Ruby
Why does Ruby have multiple control flow styles like if-else, case, modifier if/unless, and loops like while and until?
Attempts:
2 left
💡 Hint
Think about why a language would offer many ways to do the same thing.
✗ Incorrect
Ruby aims to be natural and readable. Multiple control flow styles let programmers choose the clearest way to express their logic.