Object identity (equal? vs ==) in Ruby - Performance Comparison
When comparing objects in Ruby, we want to know how fast these checks run as the program grows.
We ask: How does the time to check object equality or identity change with input size?
Analyze the time complexity of the following code snippet.
a = [1, 2, 3]
b = a
c = [1, 2, 3]
puts a.equal?(b) # true
puts a.equal?(c) # false
puts a == c # true
puts a == b # true
This code compares arrays using equal? and == to check identity and equality.
- Primary operation:
equal?checks if two variables point to the same object, which is a simple pointer check. - Primary operation:
==compares contents, which may involve checking each element in the array. - How many times:
equal?runs once per comparison, very fast. - How many times:
==may check each element, so it depends on array size.
Explain the growth pattern intuitively.
| Input Size (n) | Approx. Operations for == |
|---|---|
| 10 | About 10 element checks |
| 100 | About 100 element checks |
| 1000 | About 1000 element checks |
Pattern observation: equal? stays constant time no matter size, but == grows linearly with the number of elements.
Time Complexity: O(1) for equal?, O(n) for ==
equal? is a quick check if two variables are the same object. == takes longer as it compares each element.
[X] Wrong: "equal? and == always take the same time because they both compare objects."
[OK] Correct: equal? just checks if two things are the exact same object, which is very fast. == looks inside objects and compares each part, so it takes longer if the object is bigger.
Understanding how object identity and equality checks work helps you write efficient code and explain your choices clearly in interviews.
"What if we compare nested arrays with ==? How would the time complexity change?"
Practice
equal? check between two objects?Solution
Step 1: Understand
Theequal?method purposeequal?method checks if two variables reference the exact same object in memory, not just if they look alike.Step 2: Compare with other methods
Unlike==, which compares content,equal?is about object identity.Final Answer:
If both variables point to the exact same object in memory -> Option CQuick Check:
equal?checks object identity = D [OK]
equal? means same object, not just same value [OK]- Confusing
equal?with== - Thinking
equal?compares content - Assuming
equal?checks class type
equal? to check if two variables refer to the same object?Solution
Step 1: Recall correct syntax for
The methodequal?equal?is called on one object and passed another as an argument, likea.equal?(b).Step 2: Identify incorrect options
a == buses content equality,a.equals(b)is not a Ruby method, andequal?(a, b)is invalid syntax.Final Answer:
a.equal?(b) -> Option AQuick Check:
Correctequal?syntax = B [OK]
object.equal?(other_object) syntax [OK]- Using
==instead ofequal? - Writing
equalsinstead ofequal? - Calling
equal?as a standalone function
a = "hello" b = "hello" puts a == b puts a.equal?(b)
Solution
Step 1: Evaluate
Botha == baandbare strings with the same content "hello", soa == breturnstrue.Step 2: Evaluate
Though content is same,a.equal?(b)aandbare different string objects, soequal?returnsfalse.Final Answer:
true false -> Option BQuick Check:
==compares content,equal?compares object identity = C [OK]
== checks value, equal? checks same object [OK]- Assuming
equal?returns true for identical strings - Confusing output order
- Thinking
==checks object identity
arr1 = [1, 2, 3] arr2 = arr1 arr3 = [1, 2, 3] puts arr1.equal?(arr2) puts arr1.equal?(arr3)What is the output and why?
Solution
Step 1: Analyze
Sincearr1.equal?(arr2)arr2is assigned directly fromarr1, both point to the same object, soequal?returnstrue.Step 2: Analyze
arr1.equal?(arr3)arr3is a new array with the same content but a different object, soequal?returnsfalse.Final Answer:
true false -> Option DQuick Check:
Same object = true, same content but different object = false [OK]
- Assuming arrays with same content are the same object
- Confusing
==withequal? - Thinking assignment creates a new object
x and y refer to the same object, but x might be nil. Which Ruby code safely performs this check without errors?Solution
Step 1: Check safety of
In Ruby,equal?onnilnilis an object and responds toequal?, sox.equal?(y)is safe even ifxisnil. It returnsfalseunlessyis alsonil.Step 2: Eliminate distractors
x&.equal?(y)returnsnilifxisnil, which is not a proper boolean result.y.equals(x)calls a non-existent method.x == ychecks content, not identity.Final Answer:
x.equal?(y) -> Option AQuick Check:
nil.equal?(y)safe boolean check = C [OK]
nil.equal?(other) works without error [OK]- Using
&.which returnsnilinstead of boolean - Calling non-existent
equalsmethod - Using
==which checks content, not identity
Ready to prove it?
Pass the test and this pattern moves unknown
