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.
obj1 = "hello" obj2 = "hello" puts obj1 == obj2 puts obj1.equal?(obj2)
| Step | Action | obj1 | obj2 | obj1 == obj2 | obj1.equal?(obj2) | Output |
|---|---|---|---|---|---|---|
| 1 | Create obj1 = "hello" | "hello" (obj1) | - | - | - | - |
| 2 | Create obj2 = "hello" | "hello" (obj1) | "hello" (obj2) | - | - | - |
| 3 | Compare obj1 == obj2 | "hello" (obj1) | "hello" (obj2) | true | - | true |
| 4 | Compare obj1.equal?(obj2) | "hello" (obj1) | "hello" (obj2) | - | false | false |
| 5 | End | "hello" (obj1) | "hello" (obj2) | true | false | - |
| Variable | Start | After Step 1 | After Step 2 | Final |
|---|---|---|---|---|
| obj1 | nil | "hello" (obj1) | "hello" (obj1) | "hello" (obj1) |
| obj2 | nil | nil | "hello" (obj2) | "hello" (obj2) |
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.
equal? check between two objects?equal? method purposeequal? method checks if two variables reference the exact same object in memory, not just if they look alike.==, which compares content, equal? is about object identity.equal? checks object identity = D [OK]equal? means same object, not just same value [OK]equal? with ==equal? compares contentequal? checks class typeequal? to check if two variables refer to the same object?equal?equal? is called on one object and passed another as an argument, like a.equal?(b).a == b uses content equality, a.equals(b) is not a Ruby method, and equal?(a, b) is invalid syntax.equal? syntax = B [OK]object.equal?(other_object) syntax [OK]== instead of equal?equals instead of equal?equal? as a standalone functiona = "hello" b = "hello" puts a == b puts a.equal?(b)
a == ba and b are strings with the same content "hello", so a == b returns true.a.equal?(b)a and b are different string objects, so equal? returns false.== compares content, equal? compares object identity = C [OK]== checks value, equal? checks same object [OK]equal? returns true for identical strings== checks object identityarr1 = [1, 2, 3] arr2 = arr1 arr3 = [1, 2, 3] puts arr1.equal?(arr2) puts arr1.equal?(arr3)What is the output and why?
arr1.equal?(arr2)arr2 is assigned directly from arr1, both point to the same object, so equal? returns true.arr1.equal?(arr3)arr3 is a new array with the same content but a different object, so equal? returns false.== with equal?x and y refer to the same object, but x might be nil. Which Ruby code safely performs this check without errors?equal? on nilnil 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.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.nil.equal?(y) safe boolean check = C [OK]nil.equal?(other) works without error [OK]&. which returns nil instead of booleanequals method== which checks content, not identityPass the test and this pattern moves unknown