Bird
0
0

Why might equal? return false even if == returns true for two Ruby objects?

hard📝 Conceptual Q10 of 15
Ruby - Classes and Objects
Why might equal? return false even if == returns true for two Ruby objects?
ABecause <code>equal?</code> is deprecated and unreliable
BBecause <code>equal?</code> checks object identity, and <code>==</code> checks value equality
CBecause <code>equal?</code> compares classes, and <code>==</code> compares methods
DBecause <code>equal?</code> checks value equality, and <code>==</code> checks object identity
Step-by-Step Solution
Solution:
  1. Step 1: Define equal? and == roles

    equal? checks if two variables point to the same object in memory.
  2. Step 2: Define == role

    == checks if two objects have the same value or content, regardless of identity.
  3. Final Answer:

    equal? checks object identity, == checks value equality -> Option B
  4. Quick Check:

    equal? = identity, == = value equality [OK]
Quick Trick: equal? is identity, == is value equality [OK]
Common Mistakes:
  • Swapping meanings of equal? and ==
  • Thinking equal? is deprecated
  • Confusing class and method checks

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes