# Making Array#uniq work

**URL:** <https://rubytalk.org/t/making-array-uniq-work/24265>\
**Category:** ruby-talk\
**Created:** [24 January 2006 12:02 UTC](https://rubytalk.org/t/making-array-uniq-work/24265 "2006-01-24T12:02:21Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kroeger\_Simon\_ext](https://avatars.discourse-cdn.com/v4/letter/k/5daacb/32.png) [@Kroeger\_Simon\_ext](https://rubytalk.org/u/Kroeger_Simon_ext)\
**Post date:** [24 January 2006 12:02 UTC](https://rubytalk.org/t/making-array-uniq-work/24265/1 "2006-01-24T12:02:21Z")

</div>

Hi,

you have to define a 'hash' method because Array uses a Hash to remove  
the duplicates.

e.g.:

> **···**
>
> > -----Original Message-----  
> > From: Damphyr [mailto:damphyr@freemail.gr]  
> > Sent: Tuesday, January 24, 2006 12:35 PM  
> > To: ruby-talk ML  
> > Subject: Making Array#uniq work
> > 
> > OK, according to the Pickaxe 2nd. Edition, Array#uniq judges  
> > uniqueness  
> > based on eql?. Somehow this doesn't seem to work for me :).  
> > Following code returns the Array with two members instead of  
> > the one I  
> > would wish for. Any pointers? I'm running 1.8.2
> 
> ----------------------------------------------------------  
> class A  
> &nbsp;&nbsp;&nbsp;attr\_accessor :a,:b  
> &nbsp;&nbsp;&nbsp;def initialize a  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@a=a  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@b=Array.new  
> &nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;def \<\< item  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@b\<\<item  
> &nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;  
> &nbsp;&nbsp;&nbsp;def eql? other  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return true if @a==other.a && @b==other.b  
> &nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;  
> &nbsp;&nbsp;&nbsp;def == other  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return eql?(other)  
> &nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;  
> &nbsp;&nbsp;&nbsp;def hash  
> &nbsp;&nbsp;&nbsp;&nbsp;a.hash \* b.hash  
> &nbsp;&nbsp;&nbsp;end  
> end
> 
> o1=A.new("a")  
> o2=A.new("a")  
> o1\<\<"o"  
> o2\<\<"o"  
> puts o1==o2  
> puts o1.eql?(o2)  
> p [o1,o2].uniq  
> ----------------------------------------------------------
> 
> cheers
> 
> Simon
