Bird
0
0

Which Ruby expression correctly tests if variables x and y reference the same object?

easy📝 Syntax Q3 of 15
Ruby - Classes and Objects
Which Ruby expression correctly tests if variables x and y reference the same object?
Ax.object_id == y.class
Bx == y
Cx.eql?(y)
Dx.equal?(y)
Step-by-Step Solution
Solution:
  1. Step 1: Identify method for object identity

    The method equal? checks if two variables point to the same object.
  2. Step 2: Review other options

    == compares content, eql? is for hash key equality, and comparing object_id to class is invalid.
  3. Final Answer:

    x.equal?(y) -> Option D
  4. Quick Check:

    equal? tests object identity [OK]
Quick Trick: Use equal? to check if two variables are the same object [OK]
Common Mistakes:
  • Using == instead of equal? for object identity
  • Confusing eql? with equal?
  • Comparing object_id to class instead of object_id to object_id

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes