From: Krishna Dole Date: 2003-06-27T09:48:56+09:00 Subject: Using & for arrays of objects Hi, I'm having trouble using the & (intersection) method on two arrays of objects. I know that if the objects don't have comparison methods, & won't work because it is looking at the object IDs rather than values stored in the objects, but I can't seem to get & to use the comparison methods I want. Thanks, Krishna # A silly class that stores a single number. class Trivial def initialize(n) @n = n end def n @n end # I hoped these two methods would allow & to work on arrays containing this class. def ==(anOther) @n == anOther.n end def <=>(anOther) @n <=> anOther.n end end a = Trivial.new(6) b = Trivial.new(6) p (a == b) # this returns true, like I hoped p (a<=>b) # this returns 0, also good # Now make some arrays foo = [Trivial.new(1), Trivial.new(12)] bar = [Trivial.new(1), Trivial.new(4)] p (foo[0] == bar[0]) # returns true p (foo[0] <=> bar[0]) # returns 0 -- Great! everything seems to be working! p (foo & bar) # returns an empty array! NOOOOO!!!!!