Conditional assignment (||=) in Ruby - Time & Space Complexity
We want to understand how fast a program runs when it uses conditional assignment with ||= in Ruby.
Specifically, how does the number of steps change as the input grows?
Analyze the time complexity of the following code snippet.
def fill_defaults(data)
data.each do |key, value|
data[key] ||= expensive_computation(key)
end
end
# expensive_computation simulates a costly operation
This code goes through each item in a hash and sets a default value only if the current value is nil or false.
- Primary operation: Looping through each key-value pair in the hash.
- How many times: Once for each item in the hash (n times).
As the number of items in the hash grows, the program checks each item once.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 checks and possible assignments |
| 100 | About 100 checks and possible assignments |
| 1000 | About 1000 checks and possible assignments |
Pattern observation: The work grows directly with the number of items, so doubling items doubles the work.
Time Complexity: O(n)
This means the program takes longer in a straight line as the input size grows.
[X] Wrong: "Using ||= makes the code run faster than looping through all items."
[OK] Correct: The ||= only skips assignment if the value exists, but the loop still checks every item, so the time still grows with input size.
Understanding how conditional assignment affects performance helps you explain your code choices clearly and shows you know how programs scale.
"What if we replaced the hash with an array and used ||= inside a nested loop? How would the time complexity change?"
Practice
||= do when used like x ||= 10?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]
- Thinking ||= always assigns the value
- Confusing ||= with simple assignment =
- Assuming ||= assigns when variable is true
a only if a is nil or false?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]
- Using single | or misplaced symbols
- Confusing with assignment =
- Typing extra or missing characters
a = nil b = 20 a ||= 10 b ||= 30 puts a puts b
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]
- Assuming b changes to 30
- Thinking nil stays nil after ||= 10
- Mixing up output order
count = 0 count ||= 5 puts count
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]
- Thinking 0 is false and gets replaced
- Expecting syntax or runtime error
- Confusing Ruby truthiness with other languages
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?
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]
- Assuming 12 is replaced by 14
- Thinking ||= doesn't work on hash values
- Confusing nil with false values
Ready to prove it?
Pass the test and this pattern moves unknown
