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 rg_hash :hash
alias 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
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 rg_hash :hash
alias 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