Bird
Raised Fist0
Rubyprogramming~10 mins

Object identity (equal? vs ==) in Ruby - Visual Side-by-Side Comparison

Choose your learning style10 modes available
Concept Flow - Object identity (equal? vs ==)
Create obj1
↓
Create obj2
↓
Compare obj1 == obj2?
↓
Compare obj1.equal?(obj2)?
This flow shows creating two objects and comparing them using == and equal? to check value equality vs object identity.
Execution Sample
Ruby
obj1 = "hello"
obj2 = "hello"
puts obj1 == obj2
puts obj1.equal?(obj2)
Creates two string objects with same content, compares with == and equal? to show difference.
Execution Table
StepActionobj1obj2obj1 == obj2obj1.equal?(obj2)Output
1Create obj1 = "hello""hello" (obj1)----
2Create obj2 = "hello""hello" (obj1)"hello" (obj2)---
3Compare obj1 == obj2"hello" (obj1)"hello" (obj2)true-true
4Compare obj1.equal?(obj2)"hello" (obj1)"hello" (obj2)-falsefalse
5End"hello" (obj1)"hello" (obj2)truefalse-
💡 Comparison ends after checking both == and equal? methods.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
obj1nil"hello" (obj1)"hello" (obj1)"hello" (obj1)
obj2nilnil"hello" (obj2)"hello" (obj2)
Key Moments - 2 Insights
Why does obj1 == obj2 return true but obj1.equal?(obj2) return false?
Because == checks if the content or value is the same (see step 3), while equal? checks if both variables point to the exact same object in memory (step 4). Here, obj1 and obj2 have same content but are different objects.
Can two different objects have the same value in Ruby?
Yes, as shown in step 2 and 3, obj1 and obj2 are different objects but have identical string content, so == returns true but equal? returns false.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table at step 3, what does obj1 == obj2 evaluate to?
Atrue
Bfalse
Cnil
Derror
💡 Hint
Check the 'obj1 == obj2' column in row for step 3 in execution_table.
At which step does obj1.equal?(obj2) get evaluated?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look at the 'Action' column in execution_table to find when equal? is called.
If obj2 was assigned as obj2 = obj1, what would obj1.equal?(obj2) return?
Anil
Bfalse
Ctrue
Derror
💡 Hint
Refer to variable_tracker to see that both variables point to the same object if assigned directly.
Concept Snapshot
In Ruby:
- == checks if two objects have the same value/content.
- equal? checks if two variables point to the exact same object.
- Different objects can be == true but equal? false.
- Use equal? to test object identity, == for value equality.
Full Transcript
This example creates two string objects obj1 and obj2 with the same content "hello". When comparing with ==, Ruby checks if their values are equal, which is true. But equal? checks if both variables point to the same object in memory, which is false here because they are separate objects. This shows the difference between value equality and object identity in Ruby.

Practice

(1/5)
1. In Ruby, what does the method equal? check between two objects?
easy
A. If both objects respond to the same methods
B. If both objects have the same content or value
C. If both variables point to the exact same object in memory
D. If both objects are of the same class

Solution

  1. Step 1: Understand equal? method purpose

    The equal? method checks if two variables reference the exact same object in memory, not just if they look alike.
  2. Step 2: Compare with other methods

    Unlike ==, which compares content, equal? is about object identity.
  3. Final Answer:

    If both variables point to the exact same object in memory -> Option C
  4. Quick Check:

    equal? checks object identity = D [OK]
Hint: Remember: equal? means same object, not just same value [OK]
Common Mistakes:
  • Confusing equal? with ==
  • Thinking equal? compares content
  • Assuming equal? checks class type
2. Which of the following Ruby code snippets correctly uses equal? to check if two variables refer to the same object?
easy
A. a.equal?(b)
B. a == b
C. a.equals(b)
D. equal?(a, b)

Solution

  1. Step 1: Recall correct syntax for equal?

    The method equal? is called on one object and passed another as an argument, like a.equal?(b).
  2. Step 2: Identify incorrect options

    a == b uses content equality, a.equals(b) is not a Ruby method, and equal?(a, b) is invalid syntax.
  3. Final Answer:

    a.equal?(b) -> Option A
  4. Quick Check:

    Correct equal? syntax = B [OK]
Hint: Use object.equal?(other_object) syntax [OK]
Common Mistakes:
  • Using == instead of equal?
  • Writing equals instead of equal?
  • Calling equal? as a standalone function
3. What will be the output of the following Ruby code?
a = "hello"
b = "hello"
puts a == b
puts a.equal?(b)
medium
A. true true
B. true false
C. false true
D. false false

Solution

  1. Step 1: Evaluate a == b

    Both a and b are strings with the same content "hello", so a == b returns true.
  2. Step 2: Evaluate a.equal?(b)

    Though content is same, a and b are different string objects, so equal? returns false.
  3. Final Answer:

    true false -> Option B
  4. Quick Check:

    == compares content, equal? compares object identity = C [OK]
Hint: == checks value, equal? checks same object [OK]
Common Mistakes:
  • Assuming equal? returns true for identical strings
  • Confusing output order
  • Thinking == checks object identity
4. Consider this Ruby code snippet:
arr1 = [1, 2, 3]
arr2 = arr1
arr3 = [1, 2, 3]

puts arr1.equal?(arr2)
puts arr1.equal?(arr3)
What is the output and why?
medium
A. false false
B. false true
C. true true
D. true false

Solution

  1. Step 1: Analyze arr1.equal?(arr2)

    Since arr2 is assigned directly from arr1, both point to the same object, so equal? returns true.
  2. Step 2: Analyze arr1.equal?(arr3)

    arr3 is a new array with the same content but a different object, so equal? returns false.
  3. Final Answer:

    true false -> Option D
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    x.equal?(y) -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard