An interactive Ruby lesson
Conditions
Conditions let a program choose which code to run. Build on Variables to compare values and predict which branch Ruby takes.
p(...) displays an inspected value, with quotes around strings. Predict the output, press Run, then open the expected output to check your answer. Edit the code and run it again. Reset restores that example; Stop ends a running example.
The first Run downloads Ruby into your browser. JavaScript and a network connection are needed to run examples. Each run starts fresh, so define every variable in the editor you run. The code and answers remain readable without JavaScript.
1. Compare numbers
A comparison produces true or false. For numbers, >= means greater than or equal to, == tests equality, and < means less than. Predict each result.
Show expected output
true true false
Try it: Change coins to 4. Predict each comparison again.
2. Run code when a condition is true
if tests a condition. When it is true, Ruby runs the indented code. This part is a branch. end closes the if expression. Code after end runs next. Predict both output lines.
Show expected output
"Enough coins" "Finished"
Try it: Change coins to 4. The condition is false, so Ruby skips the branch. Predict what still prints.
3. Choose between two branches
else starts the branch that runs when the condition is false. Ruby chooses one branch, then continues after end. Predict which message prints.
Show expected output
"Need more coins"
Try it: Change coins to 5. Predict which branch runs.
4. Check conditions in order
elsif adds another condition. Ruby checks from top to bottom and runs the first branch whose condition is true. It skips the remaining tests and branches. If no condition is true, it runs else. Predict the message for 85.
Show expected output
"Silver"
Try 95, then 70. For 95, both comparisons would be true, but only the first branch runs. Why must the higher threshold come first?
5. Test false, nil, and other values
An if condition can be any value. Only false and nil are falsey in Ruby: they choose the else branch. Every other value is truthy and chooses the if branch. This includes 0 and the empty string "". Predict this result.
Show expected output
"else branch"
Try it: Replace false with nil, then 0, then "". Predict the branch before each run. A condition of 0 behaves differently from 0 >= 5, which produces false.
6. Assign the value of an if expression
An if expression produces the last value evaluated in the chosen branch. Assign that value to a variable with =. Here each branch contains one number. Predict the value assigned to price and displayed by p.
Show expected output
3
Try it: Change age to 12. Predict the new value of price.
Practice on your own
Replace the code in the last editor with a program for these tasks. Predict the output, then run it.
- Assign
85toscore. - Assign an
ifexpression's value toaward. Use"Gold"for a score of at least90,"Silver"for at least80, and"Keep practicing"otherwise. - Display
awardwithp. - Try scores of
95and70. Predict the result before each run.
Show one solution and its output
score = 85 award = if score >= 90 "Gold" elsif score >= 80 "Silver" else "Keep practicing" end p(award)
Output for 85:
"Silver"
Ruby documentation
See the Ruby 4.0 if expression documentation for branches, truthiness, and expression values. The Integer comparison documentation covers >=, ==, and <.
Return to the Ruby skill tree to choose another topic.