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.