Bird
Raised Fist0
Rubyprogramming~10 mins

Conditional assignment (||=) in Ruby - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Conditional assignment (||=)
Start
↓
Check variable truthiness
Yes|No↓
Skip assignment
↓
End
Check if variable is truthy; if not, assign new value; otherwise, keep original.
Execution Sample
Ruby
x = nil
x ||= 10
x ||= 20
puts x
Assign 10 to x if x is nil or false; then try to assign 20 but skip because x is truthy; finally print x.
Execution Table
StepVariable x beforeCondition (x truthy?)ActionVariable x afterOutput
1nilNoAssign 10 to x10
210YesSkip assignment10
310-Print x1010
💡 After step 2, x is truthy so no more assignments; step 3 prints final value.
Variable Tracker
VariableStartAfter 1After 2After 3 (final)
xnil101010
Key Moments - 2 Insights
Why doesn't x change to 20 in the second conditional assignment?
Because at step 2 in the execution_table, x is already truthy (10), so the ||= operator skips assigning 20.
What values count as falsey for ||= in Ruby?
Only nil and false are falsey; all other values, including 0 and empty strings, are truthy, so ||= skips assignment.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of x after step 1?
Anil
B10
C20
Dfalse
💡 Hint
Check the 'Variable x after' column in row for step 1.
At which step does the condition 'x truthy?' become true?
AStep 1
BStep 3
CStep 2
DNever
💡 Hint
Look at the 'Condition (x truthy?)' column in the execution_table.
If x started as false instead of nil, what would be the value of x after step 1?
A10
Bnil
Cfalse
D20
💡 Hint
Recall that ||= assigns when variable is false or nil; see step 1 action.
Concept Snapshot
Conditional assignment (||=) in Ruby:
- Syntax: variable ||= value
- Assigns value only if variable is nil or false
- Keeps original if variable is truthy
- Useful for setting defaults
- Example: x ||= 10 sets x to 10 if x is nil or false
Full Transcript
This visual trace shows how Ruby's conditional assignment operator ||= works. We start with x as nil. At step 1, since x is nil (falsey), x gets assigned 10. At step 2, x is now 10 (truthy), so the assignment to 20 is skipped. Finally, at step 3, printing x outputs 10. The key point is ||= only assigns if the variable is nil or false, otherwise it keeps the current value. This helps set default values without overwriting existing ones.

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

  1. Step 1: Understand the ||= operator

    The operator ||= assigns the right side value only if the left side variable is nil or false.
  2. Step 2: Apply to example x ||= 10

    If x is nil or false, it becomes 10; otherwise, it keeps its current value.
  3. Final Answer:

    Assigns 10 to x only if x is nil or false -> Option C
  4. 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

  1. Step 1: Identify correct operator syntax

    The correct Ruby operator for conditional assignment is ||=, not any variation with single or misplaced symbols.
  2. Step 2: Match syntax to options

    Only a ||= 5 uses ||= correctly as a ||= 5.
  3. Final Answer:

    a ||= 5 -> Option A
  4. 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

  1. Step 1: Evaluate a ||= 10 when a is nil

    Since a is nil, a ||= 10 assigns 10 to a.
  2. Step 2: Evaluate b ||= 30 when b is 20

    Since b is already 20 (truthy), b ||= 30 does not change b.
  3. Final Answer:

    10 20 -> Option D
  4. 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

  1. Step 1: Understand value of count before ||=

    count is 0, which is not nil or false in Ruby (0 is truthy).
  2. Step 2: Apply count ||= 5

    Since count is truthy (0), count ||= 5 does not change it.
  3. Final Answer:

    No error; output is 0 -> Option A
  4. Quick 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:
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
A. nil\n14 because theme is nil and font_size is replaced
B. dark\n12 because theme was nil and font_size was truthy
C. dark\n14 because both keys were nil or false
D. nil\n12 because ||= does not work on hashes

Solution

  1. Step 1: Check initial values in settings hash

    settings[:theme] is nil, settings[:font_size] is 12 (truthy).
  2. Step 2: Apply conditional assignment

    settings[:theme] ||= 'dark' sets theme to 'dark' because it was nil.
    settings[:font_size] ||= 14 does not change font_size because 12 is truthy.
  3. Final Answer:

    dark 12 -> Option B
  4. Quick 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 strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard