# Ruby == how does it works?

**URL:** <https://www.ruby-forum.com/t/ruby-how-does-it-works/66468>\
**Category:** Ruby\
**Created:** [August 26, 2006, 10:53am UTC](https://www.ruby-forum.com/t/ruby-how-does-it-works/66468 "2006-08-26T10:53:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![surendra\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ad7895/32.png) [@surendra\_s](https://www.ruby-forum.com/u/surendra_s)\
**Post date:** [August 26, 2006, 10:53am UTC](https://www.ruby-forum.com/t/ruby-how-does-it-works/66468/1 "2006-08-26T10:53:49Z")

</div>

Hi,  
Does the Ruby ‘==’ operator compares the ‘object\_ids’ of the element  
before  
descending into comparing the elements?

I know about the equal? method, but I want something like following to  
be  
compared, and am worried about efficiency.  
[x y z] and [x y z]

the arrays may be different objects, but the elements will be the same  
object.

## Thanks.

Surendra S.  
[http://ssinghi.kreeti.com](http://ssinghi.kreeti.com), [http://www.kreeti.com](http://www.kreeti.com)  
Read my blog at: [http://cuttingtheredtape.blogspot.com/](http://cuttingtheredtape.blogspot.com/)  
,----  
| Great wits are sure to madness near allied,  
| And thin partitions do their bounds divide.  
|  
| (John Dryden, Absalom and Achitophel, 1681)  
`----

---

<div class="post-metadata">

**Author:** ![surendra\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ad7895/32.png) [@surendra\_s](https://www.ruby-forum.com/u/surendra_s)\
**Post date:** [August 26, 2006, 10:59am UTC](https://www.ruby-forum.com/t/ruby-how-does-it-works/66468/2 "2006-08-26T10:59:51Z")

</div>

On 06-08-26, at 03:33, Surendra S. wrote:

> Hi,  
> Does the Ruby ‘==’ operator compares the ‘object\_ids’ of the  
> element before  
> descending into comparing the elements?

Depends on how the object receiving it implements it.

---

<div class="post-metadata">

**Author:** ![surendra\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ad7895/32.png) [@surendra\_s](https://www.ruby-forum.com/u/surendra_s)\
**Post date:** [August 27, 2006, 5:06am UTC](https://www.ruby-forum.com/t/ruby-how-does-it-works/66468/3 "2006-08-27T05:06:50Z")

</div>

Jeremy T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> On 06-08-26, at 03:33, Surendra S. wrote:
> 
> > Hi,  
> > Does the Ruby ‘==’ operator compares the ‘object\_ids’ of the  
> > element before  
> > descending into comparing the elements?
> 
> Depends on how the object receiving it implements it.

And how does hashes, and arrays implement it?

## Thanks,

Surendra S.  
[http://ssinghi.kreeti.com](http://ssinghi.kreeti.com), [http://www.kreeti.com](http://www.kreeti.com)  
Read my blog at: [http://cuttingtheredtape.blogspot.com/](http://cuttingtheredtape.blogspot.com/)  
,----  
| “O thou my friend! The prosperity of Crime is like unto the lightning,  
| whose traitorous brilliancies embellish the atmosphere but for an  
| instant, in order to hurl into death’s very depths the luckless one  
| they have dazzled.” – Marquis de Sade  
`----

---

<div class="post-metadata">

**Author:** ![surendra\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ad7895/32.png) [@surendra\_s](https://www.ruby-forum.com/u/surendra_s)\
**Post date:** [August 27, 2006, 5:22am UTC](https://www.ruby-forum.com/t/ruby-how-does-it-works/66468/4 "2006-08-27T05:22:55Z")

</div>

On 8/26/06, Jeremy T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On 06-08-26, at 03:33, Surendra S. wrote:
> 
> > Does the Ruby ‘==’ operator compares the ‘object\_ids’ of the  
> > element before
> 
> Depends on how the object receiving it implements it.

More exactly == is a method →

class Foo  
def==(other)  
self.class == other.class  
end  
end

f = Foo.new  
g = Foo.new

f == 7 =\> false  
f == f =\> true  
f == g =\> true

pth
