📖 Scenario: You are building a simple program to track user preferences for a website. Sometimes users have not set their preferences yet, so you want to assign default values only if the preferences are missing or empty.
🎯 Goal: Learn how to use the conditional assignment operator ||= in Ruby to set default values only when variables are nil or false.
📋 What You'll Learn
Create a variable theme with the value nil
Create a variable font_size with the value nil
Create a variable language with the value "English"
Use ||= to assign "Light" to theme only if it is nil or false
Use ||= to assign 12 to font_size only if it is nil or false
Use ||= to assign "English" to language only if it is nil or false
Print the values of theme, font_size, and language
💡 Why This Matters
🌍 Real World
Websites and apps often need to set default settings for users who haven't chosen their preferences yet.
💼 Career
Understanding conditional assignment helps in writing clean, efficient Ruby code for configuration and data handling.
Progress0 / 4 steps
1
Create initial variables
Create three variables: theme set to nil, font_size set to nil, and language set to "English".
Ruby
Hint
Use = to assign values to variables. nil means no value.
2
Add default values with ||= operator
Use the conditional assignment operator ||= to assign "Light" to theme, 12 to font_size, and "English" to language only if they are nil or false.
Ruby
Hint
The ||= operator assigns the value only if the variable is nil or false.
3
Check the values after conditional assignment
Write a puts statement to print the values of theme, font_size, and language separated by commas.
Ruby
Hint
Use puts with string interpolation to print variables separated by commas.
4
Run and see the output
Run the program and observe the output. It should print Light, 12, English.
Ruby
Hint
If the output is not correct, check your variable names and the use of ||=.
Practice
(1/5)
1. What does the Ruby operator ||= do when used like x ||= 10?
easy
A. Always assigns 10 to x, replacing any value
B. Assigns 10 to x only if x is true
C. Assigns 10 to x only if x is nil or false
D. Throws an error if x is already assigned
Solution
Step 1: Understand the ||= operator
The operator ||= assigns the right side value only if the left side variable is nil or false.
Step 2: Apply to example x ||= 10
If x is nil or false, it becomes 10; otherwise, it keeps its current value.
Final Answer:
Assigns 10 to x only if x is nil or false -> Option C
Quick Check:
Conditional assignment = Assign if nil/false [OK]
Hint: ||= sets value only if variable is nil or false [OK]
Common Mistakes:
Thinking ||= always assigns the value
Confusing ||= with simple assignment =
Assuming ||= assigns when variable is true
2. Which of the following is the correct syntax to assign 5 to variable a only if a is nil or false?
easy
A. a ||= 5
B. a =|| 5
C. a =| 5
D. a =|= 5
Solution
Step 1: Identify correct operator syntax
The correct Ruby operator for conditional assignment is ||=, not any variation with single or misplaced symbols.
Step 2: Match syntax to options
Only a ||= 5 uses ||= correctly as a ||= 5.
Final Answer:
a ||= 5 -> Option A
Quick Check:
Correct syntax = a ||= 5 [OK]
Hint: Remember ||= has two vertical bars before equals [OK]
Common Mistakes:
Using single | or misplaced symbols
Confusing with assignment =
Typing extra or missing characters
3. What will be the output of this Ruby code?
a = nil
b = 20
a ||= 10
b ||= 30
puts a
puts b
medium
A. 10\n30
B. nil\n30
C. nil\n20
D. 10\n20
Solution
Step 1: Evaluate a ||= 10 when a is nil
Since a is nil, a ||= 10 assigns 10 to a.
Step 2: Evaluate b ||= 30 when b is 20
Since b is already 20 (truthy), b ||= 30 does not change b.
Final Answer:
10
20 -> Option D
Quick Check:
nil replaced, truthy kept = 10 and 20 [OK]
Hint: ||= changes only nil or false variables [OK]
Common Mistakes:
Assuming b changes to 30
Thinking nil stays nil after ||= 10
Mixing up output order
4. Find the error in this Ruby code snippet:
count = 0
count ||= 5
puts count
medium
A. No error; output is 0
B. Syntax error due to ||= usage
C. Output is 5 instead of 0
D. Runtime error because count is zero
Solution
Step 1: Understand value of count before ||=
count is 0, which is not nil or false in Ruby (0 is truthy).
Step 2: Apply count ||= 5
Since count is truthy (0), count ||= 5 does not change it.
Final Answer:
No error; output is 0 -> Option A
Quick Check:
0 is truthy, ||= keeps original value [OK]
Hint: Remember 0 is truthy in Ruby, ||= won't overwrite [OK]