# Customizing Array#uniq by defining eql?

**URL:** <https://rubytalk.org/t/customizing-array-uniq-by-defining-eql/16836>\
**Category:** ruby-talk\
**Created:** [7 March 2005 20:16 UTC](https://rubytalk.org/t/customizing-array-uniq-by-defining-eql/16836 "2005-03-07T20:16:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marcel\_Molina\_Jr](https://avatars.discourse-cdn.com/v4/letter/m/edb3f5/32.png) [@Marcel\_Molina\_Jr](https://rubytalk.org/u/Marcel_Molina_Jr)\
**Post date:** [7 March 2005 20:16 UTC](https://rubytalk.org/t/customizing-array-uniq-by-defining-eql/16836/1 "2005-03-07T20:16:17Z")

</div>

PickAxe II's documentation for Array#uniq says,

&nbsp;&nbsp;&nbsp;&nbsp;Returns a new array by removing duplicate values in \_arr\_, where  
&nbsp;&nbsp;&nbsp;&nbsp;duplicates are detected by comparing using +eql?+.

Given that say I have a simple class:

&nbsp;&nbsp;&nbsp;&nbsp;class PossibleTime  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;attr\_reader :day, :start\_time, :end\_time

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def initialize(day, start\_time, end\_time)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@day, @start\_time, @end\_time = day, start\_time, end\_time  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def hash  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"#@day #@start\_time #@end\_time"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def ==(other\_pt)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.hash == other\_pt.hash  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def eql?(other\_pt)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self == other\_pt  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;end

Here +eql?+ is defined in terms of +==+. Creating two example instances shows  
these methods working:

&nbsp;&nbsp;&nbsp;&nbsp;\> a = PossibleTime.new('M', '9:30', '10:30')  
&nbsp;&nbsp;&nbsp;&nbsp;\> b = PossibleTime.new('M', '9:30', '10:30')  
&nbsp;&nbsp;&nbsp;&nbsp;\> c = PossibleTime.new('T', '9:30', '12:00')  
&nbsp;&nbsp;&nbsp;&nbsp;\> a == b  
&nbsp;&nbsp;&nbsp;&nbsp;=\> true  
&nbsp;&nbsp;&nbsp;&nbsp;\> a.eql? b  
&nbsp;&nbsp;&nbsp;&nbsp;=\> true  
&nbsp;&nbsp;&nbsp;&nbsp;\> a == c  
&nbsp;&nbsp;&nbsp;&nbsp;=\> false  
&nbsp;&nbsp;&nbsp;&nbsp;\> [a,b].uniq.size  
&nbsp;&nbsp;&nbsp;&nbsp;=\> 2  
&nbsp;&nbsp;&nbsp;&nbsp;\> # I was expecting 1, not 2

I'll add debugging to see if +eql?+ was even called:

&nbsp;&nbsp;&nbsp;&nbsp;class PossibleTime  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def eql?(other\_pt)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts 'eql? called'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self == other\_pt  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;\> a.eql? b  
&nbsp;&nbsp;&nbsp;&nbsp;eql? called  
&nbsp;&nbsp;&nbsp;&nbsp;=\> true  
&nbsp;&nbsp;&nbsp;&nbsp;\> a.eql? c  
&nbsp;&nbsp;&nbsp;&nbsp;eql? called  
&nbsp;&nbsp;&nbsp;&nbsp;=\> false  
&nbsp;&nbsp;&nbsp;&nbsp;\> [a,b].uniq.size  
&nbsp;&nbsp;&nbsp;&nbsp;=\> 2

So eql? isn't even being called.

Am I overlooking something? Specifying something incorrectly? Not  
understanding the fundamental interaction between eql? and uniq?

Thanks for your time,  
marcel

> **···**
>
> --  
> Marcel Molina Jr. \<[marcel@vernix.org](mailto:marcel@vernix.org)\>

---

<div class="post-metadata">

**Author:** ![Yukihiro\_Matsumoto2](https://avatars.discourse-cdn.com/v4/letter/y/b9e5f3/32.png) [@Yukihiro\_Matsumoto2](https://rubytalk.org/u/Yukihiro_Matsumoto2)\
**Post date:** [8 March 2005 01:21 UTC](https://rubytalk.org/t/customizing-array-uniq-by-defining-eql/16836/2 "2005-03-08T01:21:00Z")

</div>

Hi,

> **···**
>
> In message "Re: Customizing Array#uniq by defining eql?" on Tue, 8 Mar 2005 05:16:17 +0900, "Marcel Molina Jr." \<marcel@vernix.org\> writes:
> 
> > PickAxe II's documentation for Array#uniq says,
> > 
> > &nbsp;&nbsp;&nbsp;Returns a new array by removing duplicate values in \_arr\_, where  
> > &nbsp;&nbsp;&nbsp;duplicates are detected by comparing using +eql?+.
> 
> It creates hash internally to remove redundant values, so that  
> comparison is done by "eql?" but it is filtered by "hash" value first.  
> In other words, when you redefine "eql?" you have to redefine "hash"  
> as well.
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;matz.

---

<div class="post-metadata">

**Author:** ![Marcel\_Molina\_Jr](https://avatars.discourse-cdn.com/v4/letter/m/edb3f5/32.png) [@Marcel\_Molina\_Jr](https://rubytalk.org/u/Marcel_Molina_Jr)\
**Post date:** [8 March 2005 02:32 UTC](https://rubytalk.org/t/customizing-array-uniq-by-defining-eql/16836/3 "2005-03-08T02:32:37Z")

</div>

Thanks for the reply. Yes, indeed, I had noted that I needed to define  
both hash and eql?. I assumed that that was all that was needed for  
uniq to work as expected.

The pertinent bits of code from the example I posted in the original email  
are as follows:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def hash  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"#@day #@start\_time #@end\_time"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def ==(other\_pt)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.hash == other\_pt.hash  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def eql?(other\_pt)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self == other\_pt  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

And yet eql? doesn't even seem to be called.

Thanks for your time,  
marcel

> **···**
>
> On Tue, Mar 08, 2005 at 10:21:00AM +0900, Yukihiro Matsumoto wrote:
> 
> > In message "Re: Customizing Array#uniq by defining eql?" \> on Tue, 8 Mar 2005 05:16:17 +0900, "Marcel Molina Jr." \<marcel@vernix.org\> writes:
> > 
> > \>PickAxe II's documentation for Array#uniq says,  
> > \>  
> > \> Returns a new array by removing duplicate values in \_arr\_, where  
> > \> duplicates are detected by comparing using +eql?+.
> > 
> > It creates hash internally to remove redundant values, so that  
> > comparison is done by "eql?" but it is filtered by "hash" value first.  
> > In other words, when you redefine "eql?" you have to redefine "hash"  
> > as well.
> 
> --  
> Marcel Molina Jr. \<marcel@vernix.org\>

---

<div class="post-metadata">

**Author:** ![Yukihiro\_Matsumoto2](https://avatars.discourse-cdn.com/v4/letter/y/b9e5f3/32.png) [@Yukihiro\_Matsumoto2](https://rubytalk.org/u/Yukihiro_Matsumoto2)\
**Post date:** [8 March 2005 08:37 UTC](https://rubytalk.org/t/customizing-array-uniq-by-defining-eql/16836/4 "2005-03-08T08:37:44Z")

</div>

Hi,

> **···**
>
> In message "Re: Customizing Array#uniq by defining eql?" on Tue, 8 Mar 2005 11:32:37 +0900, "Marcel Molina Jr." \<marcel@vernix.org\> writes:
> 
> > Thanks for the reply. Yes, indeed, I had noted that I needed to define  
> > both hash and eql?. I assumed that that was all that was needed for  
> > uniq to work as expected.
> 
> hash method should return integer value.
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;matz.
