The equal? method checks if two variables point to the exact same object in memory. It tests object identity.
Click to reveal answer
beginner
What does the == operator check in Ruby?
The == operator checks if two objects have the same value or content, not necessarily the same object in memory.
Click to reveal answer
beginner
Given a = "hello" and b = "hello", what will a.equal?(b) return?
It will return false because a and b are two different string objects even though they have the same content.
Click to reveal answer
beginner
Given a = "hello" and b = "hello", what will a == b return?
It will return true because a and b have the same string content.
Click to reveal answer
intermediate
Why might you use equal? instead of ==?
Use equal? when you want to know if two variables are exactly the same object, not just equal in value. This is important when object identity matters.
Click to reveal answer
In Ruby, which method checks if two variables refer to the same object?
Aequal?
B==
Ceql?
Dequals
✗ Incorrect
The equal? method checks if two variables point to the exact same object in memory.
What does a == b check in Ruby?
AIf a and b are the same object
BIf a and b have different object ids
CIf a and b have the same value
DIf a and b are different classes
✗ Incorrect
The == operator checks if two objects have the same value or content.
If a = "cat" and b = a, what will a.equal?(b) return?
Atrue
Bfalse
Cnil
Derror
✗ Incorrect
Since b points to the same object as a, equal? returns true.
Which method would return false for two different string objects with the same content?
Aclone
B==
Cto_s
Dequal?
✗ Incorrect
equal? returns false because the objects are different, even if their content is the same.
What is the main difference between equal? and == in Ruby?
Since arr2 is assigned directly from arr1, both point to the same object, so equal? returns true.
Step 2: Analyze arr1.equal?(arr3)
arr3 is a new array with the same content but a different object, so equal? returns false.
Final Answer:
true
false -> Option D
Quick Check:
Same object = true, same content but different object = false [OK]
Hint: Assignment copies object reference; new array creates new object [OK]
Common Mistakes:
Assuming arrays with same content are the same object
Confusing == with equal?
Thinking assignment creates a new object
5. You want to check if two variables x and y refer to the same object, but x might be nil. Which Ruby code safely performs this check without errors?
hard
A. x.equal?(y)
B. y.equals(x)
C. x&.equal?(y)
D. x == y
Solution
Step 1: Check safety of equal? on nil
In Ruby, nil is an object and responds to equal?, so x.equal?(y) is safe even if x is nil. It returns false unless y is also nil.
Step 2: Eliminate distractors
x&.equal?(y) returns nil if x is nil, which is not a proper boolean result. y.equals(x) calls a non-existent method. x == y checks content, not identity.
Final Answer:
x.equal?(y) -> Option A
Quick Check:
nil.equal?(y) safe boolean check = C [OK]
Hint: Even nil.equal?(other) works without error [OK]
Common Mistakes:
Using &. which returns nil instead of boolean
Calling non-existent equals method
Using == which checks content, not identity
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.