# Array.equal?

**URL:** <https://rubytalk.org/t/array-equal/50059>\
**Category:** ruby-talk\
**Created:** [7 November 2008 14:18 UTC](https://rubytalk.org/t/array-equal/50059 "2008-11-07T14:18:05Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nick\_Bhanji](https://avatars.discourse-cdn.com/v4/letter/n/c68b51/32.png) [@Nick\_Bhanji](https://rubytalk.org/u/Nick_Bhanji)\
**Post date:** [7 November 2008 14:18 UTC](https://rubytalk.org/t/array-equal/50059/1 "2008-11-07T14:18:05Z")

</div>

Hello All,

I am not sure where to ask this, but someone after reading this can tell  
me more.

I am trying to right a functions where I am comparing 2 arrays. Here is  
an example.

a = [1,1]  
b = [1,1]

in the irb, I executed a.equal? b and the result came as false.

here is the output:  
irb(main):031:0\> a =[1,1]  
=\> [1, 1]  
irb(main):032:0\> b =[1,1]  
=\> [1, 1]  
irb(main):033:0\> a.equal? b  
=\> false  
irb(main):034:0\>

however when I execute a.eql? b the result is true.

My questions:  
1. why is equal? listed in the methods for the array  
2. what is the definition of equal? in the case of array?  
3. why is there another function eql? performing the actual equal test

I am using ruby version 1.8.6 for the windows

Thank you in advance.

Nick.B

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [7 November 2008 14:26 UTC](https://rubytalk.org/t/array-equal/50059/2 "2008-11-07T14:26:22Z")

</div>

My questions:

1. What conclusions did you draw from your test results?  
2. What did the documentation say?

robert

> **···**
>
> 2008/11/7 Nick Bhanji \<nickbh@weassemble4u.ca\>:
> 
> > My questions:  
> > 1. why is equal? listed in the methods for the array  
> > 2. what is the definition of equal? in the case of array?  
> > 3. why is there another function eql? performing the actual equal test
> 
> --  
> remember.guy do |as, often| as.you\_can - without end

---

<div class="post-metadata">

**Author:** ![Sebastian\_Hungereck1](https://avatars.discourse-cdn.com/v4/letter/s/3ab097/32.png) [@Sebastian\_Hungereck1](https://rubytalk.org/u/Sebastian_Hungereck1)\
**Post date:** [7 November 2008 14:39 UTC](https://rubytalk.org/t/array-equal/50059/3 "2008-11-07T14:39:13Z")

</div>

Nick Bhanji wrote:

> My questions:  
> 1. why is equal? listed in the methods for the array  
> 2. what is the definition of equal? in the case of array?  
> 3. why is there another function eql? performing the actual equal test

1. Where exactly is it listed? It's not listed in the docs for Array.  
equal? is defined by Object and it is not overridden for any core class.  
2. The same as for anything else: a.equal? b is true if and only if a and b  
are actually the same object (i.e. both a and b point to the same location in  
memory)  
3. eql? does not perform the "actual equal test" - whatever that is. eql? is  
to be used together with hash (and is indeed used that way by Hash, Set and  
Array#&, Array#| and Array#-).  
4. The method that I assume you're looking for is ==

HTH,  
Sebastian

> **···**
>
> --  
> Jabber: sepp2k@jabber.org  
> ICQ: 205544826

---

<div class="post-metadata">

**Author:** ![Michael\_Morin](https://avatars.discourse-cdn.com/v4/letter/m/d78d45/32.png) [@Michael\_Morin](https://rubytalk.org/u/Michael_Morin)\
**Post date:** [7 November 2008 19:55 UTC](https://rubytalk.org/t/array-equal/50059/4 "2008-11-07T19:55:53Z")

</div>

Nick Bhanji wrote:

> Hello All,
> 
> I am not sure where to ask this, but someone after reading this can tell  
> me more.
> 
> I am trying to right a functions where I am comparing 2 arrays. Here is  
> an example.
> 
> a = [1,1]  
> b = [1,1]
> 
> in the irb, I executed a.equal? b and the result came as false.
> 
> here is the output:  
> irb(main):031:0\> a =[1,1]  
> =\> [1, 1]  
> irb(main):032:0\> b =[1,1]  
> =\> [1, 1]  
> irb(main):033:0\> a.equal? b  
> =\> false  
> irb(main):034:0\>
> 
> however when I execute a.eql? b the result is true.
> 
> My questions:  
> 1. why is equal? listed in the methods for the array  
> 2. what is the definition of equal? in the case of array?  
> 3. why is there another function eql? performing the actual equal test
> 
> I am using ruby version 1.8.6 for the windows
> 
> Thank you in advance.
> 
> Nick.B

The equal? method comes from the Object class. The equal? method will return true if and only if the two objects are the same object. The two lists are two independently created lists, so this will be false. The eql? method is an alias for this method.

If you want to see if two arrays contain the same objects, use the == operator.

a = [1,1]  
b = [1,1]  
print "yes" if a == b

---

<div class="post-metadata">

**Author:** ![Nick\_Bhanji](https://avatars.discourse-cdn.com/v4/letter/n/c68b51/32.png) [@Nick\_Bhanji](https://rubytalk.org/u/Nick_Bhanji)\
**Post date:** [7 November 2008 14:54 UTC](https://rubytalk.org/t/array-equal/50059/5 "2008-11-07T14:54:23Z")

</div>

Robert Klemme wrote:

> **···**
>
> > 2008/11/7 Nick Bhanji \<nickbh@weassemble4u.ca\>:
> > 
> > > My questions:  
> > > 1. why is equal? listed in the methods for the array  
> > > 2. what is the definition of equal? in the case of array?  
> > > 3. why is there another function eql? performing the actual equal test
> > 
> > My questions:
> > 
> > 1. What conclusions did you draw from your test results?  
> > 2. What did the documentation say?
> > 
> > robert
> 
> Since I went through the documentation for the Array. equal? method  
> does not exist. eql? does exist. Prior to this -- I used [1,1].methods  
> and it listed the methods that are available for the object. Now my  
> conclusion is that no to trust methods function -- because it does not  
> do what it is supposed to do -- that is allow a programmer to know what  
> methods are supported.  
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Lloyd\_Linklater](https://avatars.discourse-cdn.com/v4/letter/l/919ad9/32.png) [@Lloyd\_Linklater](https://rubytalk.org/u/Lloyd_Linklater)\
**Post date:** [7 November 2008 15:00 UTC](https://rubytalk.org/t/array-equal/50059/6 "2008-11-07T15:00:09Z")

</div>

Robert Klemme wrote:

> **···**
>
> > 2008/11/7 Nick Bhanji \<nickbh@weassemble4u.ca\>:
> > 
> > > My questions:  
> > > 1. why is equal? listed in the methods for the array  
> > > 2. what is the definition of equal? in the case of array?  
> > > 3. why is there another function eql? performing the actual equal test
> > 
> > My questions:
> > 
> > 1. What conclusions did you draw from your test results?  
> > 2. What did the documentation say?
> > 
> > robert
> 
> Nicely done, Robert!  
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Nick\_Bhanji](https://avatars.discourse-cdn.com/v4/letter/n/c68b51/32.png) [@Nick\_Bhanji](https://rubytalk.org/u/Nick_Bhanji)\
**Post date:** [7 November 2008 15:01 UTC](https://rubytalk.org/t/array-equal/50059/7 "2008-11-07T15:01:30Z")

</div>

Sebastian Hungerecker wrote:

> Nick Bhanji wrote:
> 
> > My questions:  
> > 1. why is equal? listed in the methods for the array  
> > 2. what is the definition of equal? in the case of array?  
> > 3. why is there another function eql? performing the actual equal test
> 
> 1. Where exactly is it listed? It's not listed in the docs for Array.  
> equal? is defined by Object and it is not overridden for any core class.  
> 2. The same as for anything else: a.equal? b is true if and only if a  
> and b  
> are actually the same object (i.e. both a and b point to the same  
> location in  
> memory)

you are correct to say that equal? is not in the Array docs. However,  
when I executed  
a = [1,1]  
a.methods

equal? method is there on the list of methods supported by the object  
Array that is used by the variable a.

> 3. eql? does not perform the "actual equal test" - whatever that is.  
> eql? is  
> to be used together with hash (and is indeed used that way by Hash, Set  
> and

"array.eql?(other) → true or false  
Returns true if array and other are the same object, or are both arrays  
with the same content. " --- from  
[http://ruby-doc.org/core/classes/Array.html](http://ruby-doc.org/core/classes/Array.html)

> **···**
>
> > Array#&, Array#| and Array#-).  
> > 4. The method that I assume you're looking for is ==
> > 
> > HTH,  
> > Sebastian
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Sebastian\_Hungereck1](https://avatars.discourse-cdn.com/v4/letter/s/3ab097/32.png) [@Sebastian\_Hungereck1](https://rubytalk.org/u/Sebastian_Hungereck1)\
**Post date:** [7 November 2008 20:00 UTC](https://rubytalk.org/t/array-equal/50059/8 "2008-11-07T20:00:25Z")

</div>

Michael Morin wrote:

> The eql? method is an alias for [equal?].

Not on Array, it's not. Array#eql? behaves like Array#==, not like  
Object#equal?.

> **···**
>
> --  
> NP: Depeche Mode - Lilian  
> Jabber: sepp2k@jabber.org  
> ICQ: 205544826

---

<div class="post-metadata">

**Author:** ![Nick\_Bhanji](https://avatars.discourse-cdn.com/v4/letter/n/c68b51/32.png) [@Nick\_Bhanji](https://rubytalk.org/u/Nick_Bhanji)\
**Post date:** [7 November 2008 15:12 UTC](https://rubytalk.org/t/array-equal/50059/9 "2008-11-07T15:12:27Z")

</div>

Nick Bhanji wrote:

> Sebastian Hungerecker wrote:
> 
> > Nick Bhanji wrote:
> > 
> > > My questions:  
> > > 1. why is equal? listed in the methods for the array  
> > > 2. what is the definition of equal? in the case of array?  
> > > 3. why is there another function eql? performing the actual equal test
> > 
> > 1. Where exactly is it listed? It's not listed in the docs for Array.  
> > equal? is defined by Object and it is not overridden for any core class.  
> > 2. The same as for anything else: a.equal? b is true if and only if a  
> > and b  
> > are actually the same object (i.e. both a and b point to the same  
> > location in  
> > memory)
> 
> you are correct to say that equal? is not in the Array docs. However,  
> when I executed  
> a = [1,1]  
> a.methods
> 
> equal? method is there on the list of methods supported by the object  
> Array that is used by the variable a.
> 
> > 3. eql? does not perform the "actual equal test" - whatever that is.  
> > eql? is  
> > to be used together with hash (and is indeed used that way by Hash, Set  
> > and
> 
> "array.eql?(other) → true or false  
> Returns true if array and other are the same object, or are both arrays  
> with the same content. " --- from  
> [class Array - RDoc Documentation](http://ruby-doc.org/core/classes/Array.html)
> 
> > Array#&, Array#| and Array#-).  
> > 4. The method that I assume you're looking for is ==
> > 
> > HTH,  
> > Sebastian

I did find the reference page that you probably used to explain. I got  
the reference that explains my imprise definition of equality that you  
were trying to explain.

> **[Identity and Equality in Ruby and Smalltalk](https://kentreis.wordpress.com/2007/02/08/identity-and-equality-in-ruby-and-smalltalk/)**
>
> One important concept in object-oriented languages is the difference between equality and identity. The concept isn’t complicated, but the English words we use to talk about it are imprecise.…

thank you everyone.

Nick.B

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Sebastian\_Hungereck1](https://avatars.discourse-cdn.com/v4/letter/s/3ab097/32.png) [@Sebastian\_Hungereck1](https://rubytalk.org/u/Sebastian_Hungereck1)\
**Post date:** [7 November 2008 15:24 UTC](https://rubytalk.org/t/array-equal/50059/10 "2008-11-07T15:24:21Z")

</div>

Nick Bhanji wrote:

> Now my  
> conclusion is that no to trust methods function -- because it does not  
> do what it is supposed to do -- that is allow a programmer to know what  
> methods are supported.

Wtf? .methods lists equal? because equal? is defined by Object and is as  
such available to be called on all objects including arrays. It is not listed  
in the docs of Array because it is defined by Object, not Array, and is not  
redefined by Array either. You can perfectly well call equal? on an array and  
it will behave like the docs for Object#equal? say it will.

HTH,  
Sebastian

> **···**
>
> --  
> Jabber: sepp2k@jabber.org  
> ICQ: 205544826

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [8 November 2008 13:09 UTC](https://rubytalk.org/t/array-equal/50059/11 "2008-11-08T13:09:58Z")

</div>

The statement isn't true even in the \_general case\_: eql? and ==  
implement the mathematical concept of "equivalence" and have usually  
the same semantics (there are some noteworthy differences for  
numbers). eql? being the more important method as it is also used by  
Hash and others to find equivalent entires. equal? on the contrary  
implements the concept of "identity". The default behavior of this in  
class Object should never be overridden - but then again, the method  
is used so sparingly that it probably won't matter. 🙂

Kind regards

robert

> **···**
>
> 2008/11/7 Sebastian Hungerecker \<sepp2k@googlemail.com\>:
> 
> > Michael Morin wrote:
> > 
> > > The eql? method is an alias for [equal?].
> > 
> > Not on Array, it's not. Array#eql? behaves like Array#==, not like  
> > Object#equal?.
> 
> --  
> remember.guy do |as, often| as.you\_can - without end

---

<div class="post-metadata">

**Author:** ![Nick\_Bhanji](https://avatars.discourse-cdn.com/v4/letter/n/c68b51/32.png) [@Nick\_Bhanji](https://rubytalk.org/u/Nick_Bhanji)\
**Post date:** [7 November 2008 15:28 UTC](https://rubytalk.org/t/array-equal/50059/12 "2008-11-07T15:28:06Z")

</div>

Sebastian Hungerecker wrote:

> Nick Bhanji wrote:
> 
> > Now my  
> > conclusion is that no to trust methods function -- because it does not  
> > do what it is supposed to do -- that is allow a programmer to know what  
> > methods are supported.
> 
> Wtf? .methods lists equal? because equal? is defined by Object and is  
> as  
> such available to be called on all objects including arrays. It is not  
> listed  
> in the docs of Array because it is defined by Object, not Array, and is  
> not  
> redefined by Array either. You can perfectly well call equal? on an  
> array and  
> it will behave like the docs for Object#equal? say it will.
> 
> HTH,  
> Sebastian

You gotta chill, I am trying to use a language -- not get anyone riled  
up..  
smile and be happy

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Sebastian\_Hungereck1](https://avatars.discourse-cdn.com/v4/letter/s/3ab097/32.png) [@Sebastian\_Hungereck1](https://rubytalk.org/u/Sebastian_Hungereck1)\
**Post date:** [7 November 2008 15:33 UTC](https://rubytalk.org/t/array-equal/50059/13 "2008-11-07T15:33:33Z")

</div>

Nick Bhanji wrote:

> You gotta chill,

I am chilled. My "wtf" was an expression of confusion, not anger or "riled  
up-ness".

> **···**
>
> --  
> Jabber: sepp2k@jabber.org  
> ICQ: 205544826

---

<div class="post-metadata">

**Author:** ![Nick\_Bhanji](https://avatars.discourse-cdn.com/v4/letter/n/c68b51/32.png) [@Nick\_Bhanji](https://rubytalk.org/u/Nick_Bhanji)\
**Post date:** [7 November 2008 15:37 UTC](https://rubytalk.org/t/array-equal/50059/14 "2008-11-07T15:37:19Z")

</div>

Sebastian Hungerecker wrote:

> Nick Bhanji wrote:
> 
> > You gotta chill,
> 
> I am chilled. My "wtf" was an expression of confusion, not anger or  
> "riled  
> up-ness".

We're cool -- have a wonderful day

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
