Bird
0
0

Why does the conditional assignment operator ||= treat 0 as truthy in Ruby?

hard📝 Conceptual Q10 of 15
Ruby - Operators and Expressions
Why does the conditional assignment operator ||= treat 0 as truthy in Ruby?
ABecause only nil and false are falsy in Ruby
BBecause 0 is considered false in Ruby
CBecause ||= treats all numbers as falsy
DBecause 0 is treated as nil internally
Step-by-Step Solution
Solution:
  1. Step 1: Recall Ruby's falsy values

    In Ruby, only nil and false are falsy.
  2. Step 2: Understand 0's truthiness

    0 is a valid number and considered truthy, so ||= does not assign.
  3. Final Answer:

    Because only nil and false are falsy in Ruby -> Option A
  4. Quick Check:

    Only nil/false falsy; 0 is truthy [OK]
Quick Trick: Only nil and false are falsy in Ruby [OK]
Common Mistakes:
MISTAKES
  • Assuming 0 is falsy like in other languages
  • Thinking ||= treats numbers differently
  • Confusing falsy values with zero

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes