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

  • Thread starter Wolfgang Nádasi-Donner
  • Start date
W

Wolfgang Nádasi-Donner

I am confused about "Array#uniq". In
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?

class String
alias :eek:rg_hash :hash
alias :eek:rg_eql? :eql?
def hash
puts 'String#hash called'
self.hash
end
def eql?(other)
puts 'String#eql? called'
self.eql?(other)
end
end

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

Start of test
["a", "b", "c"]
End of test

Wolfgang Nádasi-Donner
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top