Bird
0
0

Why does Ruby treat only false and nil as falsy, unlike some other languages that treat 0 or empty strings as falsy?

hard📝 Conceptual Q10 of 15
Ruby - Operators and Expressions
Why does Ruby treat only false and nil as falsy, unlike some other languages that treat 0 or empty strings as falsy?
ABecause 0 and empty strings are considered errors in Ruby
BTo simplify conditional checks and avoid unexpected false negatives
CRuby does not have a concept of truthy or falsy values
DTo make all objects behave identically in conditionals
Step-by-Step Solution
Solution:
  1. Step 1: Understand Ruby's design choice

    Ruby treats only false and nil as falsy to keep conditionals simple and predictable.
  2. Step 2: Compare with other languages

    Other languages treat 0 or empty strings as falsy, which can cause unexpected behavior. Ruby avoids this to reduce bugs.
  3. Final Answer:

    To simplify conditional checks and avoid unexpected false negatives -> Option B
  4. Quick Check:

    Ruby falsy = false and nil only for clarity [OK]
Quick Trick: Only false and nil are falsy to keep logic simple [OK]
Common Mistakes:
  • Thinking 0 or empty string are falsy
  • Believing Ruby lacks truthy/falsy
  • Assuming all objects behave same

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes