Bird
0
0

Find the error in this Ruby code snippet:

medium📝 Debug Q14 of 15
Ruby - Operators and Expressions
Find the error in this Ruby code snippet:
count = 0
count ||= 5
puts count
ANo error; output is 0
BSyntax error due to ||= usage
COutput is 5 instead of 0
DRuntime error because count is zero
Step-by-Step Solution
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]
Quick Trick: 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

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes