# Array#uniq - Comparison doesn't use 'eql?' and 'hash'

**URL:** <https://www.ruby-forum.com/t/array-uniq-comparison-doesnt-use-eql-and-hash/86154>\
**Category:** Ruby\
**Created:** [February 20, 2007, 8:25am UTC](https://www.ruby-forum.com/t/array-uniq-comparison-doesnt-use-eql-and-hash/86154 "2007-02-20T08:25:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![wonado](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/919ad9/32.png) [@wonado](https://www.ruby-forum.com/u/wonado)\
**Post date:** [February 20, 2007, 8:25am UTC](https://www.ruby-forum.com/t/array-uniq-comparison-doesnt-use-eql-and-hash/86154/1 "2007-02-20T08:25:45Z")

</div>

I am confused about “Array#uniq”. In  
[http://groups.google.com/group/comp.lang.ruby/msg/e57b80fbcd61aaab](http://groups.google.com/group/comp.lang.ruby/msg/e57b80fbcd61aaab) it is  
described, that one has to redefine “eql?” and “hash”, if one needs an  
own  
“Array#uniq” interpretation.

I made tests with the result, that neither “eql?” nor “hash” is called.  
What  
went wrong here?

> > > > > code \>\>\>\>\>

class String  
alias :org\_hash :hash  
alias :org\_eql? :eql?  
def hash  
puts ‘String#hash called’  
self.org\_hash  
end  
def eql?(other)  
puts ‘String#eql? called’  
self.org\_eql?(other)  
end  
end

puts ‘Start of test’  
a = [‘a’, ‘b’, ‘a’, ‘c’, ‘b’]  
b = a.uniq  
p b  
puts ‘End of test’

> > > > > Output \>\>\>\>\>

Start of test  
[“a”, “b”, “c”]  
End of test

> > > > > EoE \>\>\>\>\>

Wolfgang Nádasi-Donner

---

<div class="post-metadata">

**Author:** ![wonado](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/919ad9/32.png) [@wonado](https://www.ruby-forum.com/u/wonado)\
**Post date:** [February 20, 2007, 11:01am UTC](https://www.ruby-forum.com/t/array-uniq-comparison-doesnt-use-eql-and-hash/86154/2 "2007-02-20T11:01:07Z")

</div>

Wolfgang NÃ¡dasi-Donner:

> I am confused about “Array#uniq”.

Although you already have the answer on dclr, for this list’s archives:  
Strings are handled in a special way, as shown here:

definitions = lambda do

alias :org\_equal\_op :==  
def ==(other)  
puts ‘== called’  
org\_equal\_op(other)  
end

alias :org\_hash :hash  
def hash  
puts ‘hash called’  
org\_hash  
end

alias :org\_eql? :eql?  
def eql?(other)  
puts ‘eql? called’  
org\_eql?(other)  
end

alias :org\_equal? :equal?  
def equal?(other)  
puts ‘equal? called’  
org\_equal?(other)  
end

end

String.module\_eval &definitions  
Regexp.module\_eval &definitions

puts ‘Regexp:’  
p [/a/, /b/, /a/, /c/, /b/].uniq  
puts ‘String:’  
p %w(a b a c b).uniq

puts “\nRegards, Kalman”

**END**

Output:

Regexp:  
hash called  
hash called  
hash called  
eql? called  
hash called  
hash called  
eql? called  
hash called  
hash called  
hash called  
hash called  
hash called  
[/a/, /b/, /c/]  
String:  
[“a”, “b”, “c”]

Regards, Kalman

---

<div class="post-metadata">

**Author:** ![wonado](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/919ad9/32.png) [@wonado](https://www.ruby-forum.com/u/wonado)\
**Post date:** [February 20, 2007, 2:00pm UTC](https://www.ruby-forum.com/t/array-uniq-comparison-doesnt-use-eql-and-hash/86154/3 "2007-02-20T14:00:29Z")

</div>

Kalman N. schrieb:

> Although you already have the answer on dclr, for this list’s archives:  
> Strings are handled in a special way, as shown here:…

Then another question came up.

There is one good reason to use class “String” in ad-hoc-tools, instead  
of a new  
class, that uses class “String”: The constants.

If I write write

a = “this is a string”

an object of class “String” will be created. If I have my wrapper class  
“MyString” which uses class “String”, I must use some construct like

a = MyString.new(“this is my string”)

Is there some possibility to shorten this?

Wolfgang NÃ¡dasi-Donner

---

<div class="post-metadata">

**Author:** ![wonado](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/919ad9/32.png) [@wonado](https://www.ruby-forum.com/u/wonado)\
**Post date:** [February 20, 2007, 2:42pm UTC](https://www.ruby-forum.com/t/array-uniq-comparison-doesnt-use-eql-and-hash/86154/4 "2007-02-20T14:42:36Z")

</div>

A lot of often used add-on classes use the class method [], for example  
Set:  
Set[4, 3] == Set.new(4,3)

Also, you could do  
[4, 3].to\_set

Dan
