Conditional assignment helps you set a value only if the variable is nil or false. It keeps your code simple and avoids overwriting existing values.
Conditional assignment (||=) in Ruby
Introduction
Syntax
Ruby
variable ||= value
This means: assign value to variable only if variable is nil or false.
It is a shortcut for variable = variable || value.
Examples
name is nil or false, it becomes "Guest". Otherwise, it keeps its current value.Ruby
name ||= "Guest"count is nil or false, it becomes 0.Ruby
count ||= 0settings[:theme] is nil or false.Ruby
settings[:theme] ||= "light"Sample Program
First, name is nil, so it gets assigned "Alice". Then, since name already has "Alice", it does not change to "Bob".
Ruby
name = nil name ||= "Alice" puts name name ||= "Bob" puts name
Important Notes
Remember, ||= only assigns if the variable is nil or false. If the variable has any other value, it stays unchanged.
This operator is very useful for setting default values in a clean way.
Summary
Conditional assignment (||=) sets a variable only if it is nil or false.
It helps keep existing values and only fills in defaults when needed.
Use it to write shorter and clearer code for default values.
Practice
1. What does the Ruby operator
||= do when used like x ||= 10?easy
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
Ifx ||= 10xis 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 CQuick 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
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 asa ||= 5.Final Answer:
a ||= 5 -> Option AQuick 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
Solution
Step 1: Evaluate
Sincea ||= 10when a is nilais nil,a ||= 10assigns 10 toa.Step 2: Evaluate
Sinceb ||= 30when b is 20bis already 20 (truthy),b ||= 30does not changeb.Final Answer:
10 20 -> Option DQuick 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
Solution
Step 1: Understand value of count before ||=
countis 0, which is not nil or false in Ruby (0 is truthy).Step 2: Apply
Sincecount ||= 5countis truthy (0),count ||= 5does not change it.Final Answer:
No error; output is 0 -> Option AQuick Check:
0 is truthy, ||= keeps original value [OK]
Hint: Remember 0 is truthy in Ruby, ||= won't overwrite [OK]
Common Mistakes:
- Thinking 0 is false and gets replaced
- Expecting syntax or runtime error
- Confusing Ruby truthiness with other languages
5. Given this code:
What is the output and why?
settings = { theme: nil, font_size: 12 }
settings[:theme] ||= 'dark'
settings[:font_size] ||= 14
puts settings[:theme]
puts settings[:font_size]What is the output and why?
hard
Solution
Step 1: Check initial values in settings hash
settings[:theme]is nil,settings[:font_size]is 12 (truthy).Step 2: Apply conditional assignment
settings[:theme] ||= 'dark'sets theme to 'dark' because it was nil.settings[:font_size] ||= 14does not change font_size because 12 is truthy.Final Answer:
dark 12 -> Option BQuick Check:
Nil replaced, truthy kept = dark and 12 [OK]
Hint: ||= sets defaults only if current value is nil or false [OK]
Common Mistakes:
- Assuming 12 is replaced by 14
- Thinking ||= doesn't work on hash values
- Confusing nil with false values
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
