# Array#uniq with Hash elements... can't remove duplicates

**URL:** <https://www.ruby-forum.com/t/array-uniq-with-hash-elements-cant-remove-duplicates/137319>\
**Category:** Ruby\
**Created:** [May 12, 2008, 12:10am UTC](https://www.ruby-forum.com/t/array-uniq-with-hash-elements-cant-remove-duplicates/137319 "2008-05-12T00:10:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![andrea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c37758/32.png) [@andrea](https://www.ruby-forum.com/u/andrea)\
**Post date:** [May 12, 2008, 12:10am UTC](https://www.ruby-forum.com/t/array-uniq-with-hash-elements-cant-remove-duplicates/137319/1 "2008-05-12T00:10:46Z")

</div>

Hello,

I am having some trouble trying to remove duplicate data from an array  
of hashes. I’ve read on the pickaxe book that Array#uniq detects  
duplicates using the eql? method on the elements, but it doesn’t seem to  
work even if I monkeypatch the Hash class:

class Hash  
def eql? other  
self == other  
end  
end

a={:foo=\>‘bar’}  
b={:foo=\>‘bar’}  
array=[a,b]

a.eql? b  
=\> true  
array.uniq  
=\> [{:foo=\>“bar”}, {:foo=\>“bar”}]

Of course, I’d like to get only [{:foo=\>"bar}] as a result. Thanks in  
advance for any help…

Andrea

–

> **[Andrea's Retrocomputing Homepage](http://myretrocomputing.altervista.org)**
>
> Sito personale di retrocomputing a cura di Longhi Andrea

---

<div class="post-metadata">

**Author:** ![andrea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c37758/32.png) [@andrea](https://www.ruby-forum.com/u/andrea)\
**Post date:** [May 12, 2008, 12:55am UTC](https://www.ruby-forum.com/t/array-uniq-with-hash-elements-cant-remove-duplicates/137319/2 "2008-05-12T00:55:50Z")

</div>

andrea pisze:

> end  
> end

you also need to define #hash method:

class Hash  
def eql? other  
self == other  
end

def hash  
h = 0  
each\_pair do |k, v|  
h ^= k.hash  
h \*=137  
h ^= v.hash  
end  
h  
end  
end

lopex

---

<div class="post-metadata">

**Author:** ![andrea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c37758/32.png) [@andrea](https://www.ruby-forum.com/u/andrea)\
**Post date:** [May 12, 2008, 1:20am UTC](https://www.ruby-forum.com/t/array-uniq-with-hash-elements-cant-remove-duplicates/137319/3 "2008-05-12T01:20:58Z")

</div>

andrea:

> I’ve read on the pickaxe book that Array#uniq detects duplicates using  
> the eql? method on the elements, but it doesn’t seem to work even if  
> I monkeypatch the Hash class:

> class Hash  
> def eql? other  
> self == other  
> end  
> end

Hashes of the objects must also match:

shot@devielle:~$ irb

> > class Hash  
> > def hash  
> > to\_a.hash  
> > end  
> > alias eql? ==  
> > ?\> end  
> > =\> nil
> 
> > a = {:foo =\> ‘bar’}  
> > =\> {:foo=\>“bar”}
> 
> > b = {:foo =\> ‘bar’}  
> > =\> {:foo=\>“bar”}
> 
> > [a,b].uniq  
> > =\> [{:foo=\>“bar”}]

That said, monkeypatching Hash in this way is a (very) bad idea.

– Shot

---

<div class="post-metadata">

**Author:** ![andrea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c37758/32.png) [@andrea](https://www.ruby-forum.com/u/andrea)\
**Post date:** [May 12, 2008, 10:36am UTC](https://www.ruby-forum.com/t/array-uniq-with-hash-elements-cant-remove-duplicates/137319/4 "2008-05-12T10:36:12Z")

</div>

Marcin Miel?y?ski [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> you also need to define #hash method:

Thank you !

Andrea

–

> **[Andrea's Retrocomputing Homepage](http://myretrocomputing.altervista.org)**
>
> Sito personale di retrocomputing a cura di Longhi Andrea
