class Foo
include Comparable
def initialize (name, num)
@name = name
@num = num
end
attr_reader :name , :num
def to_s
"#{@name} - #{@num}"
end
def <=> (other)
if ((self.name <=> other.name) == 0)
self.num <=> other.num
else
self.name <=> other.name
end
end
end
f = []
f << Foo.new("Banzai", 1) << Foo.new("Banzai", 2) << Foo.new("Ayaken", 3)
puts f
Banzai - 1
Banzai - 2
Banzai - 2
Ayaken - 3
f << Foo.new("Banzai", 2)
puts f
Banzai - 1
Banzai - 2
Banzai - 2
Ayaken - 3
Banzai - 2
f.sort!
puts f
Ayaken - 3
Banzai - 1
Banzai - 2
Banzai - 2
Banzai - 2
f.uniq!
=> nil
Okay. So what am I doing wrong ? What does uniq rely on ? (ri nor programming
ruby could tell me ;) I thought with defining <=> and mixin with comparable I
did the job to tell my array what it has to use to find out if something is
same. Sorting goes fine, so I think I did not do major mistakes. But uniq
simply Does Not Work. I wonder if I'm a bit of blind on one eye, but that
surely does not follow the principle of the least surprise .. I *WAS*
surprised ;>
Martin Weber, tia