From: Ben Giddings Date: 2003-10-01T12:49:21+09:00 Subject: Re: Making == symmetric? Nathan Weston wrote: > def compare(a,b) > if a.can_compare_to?(b) > return a.compare_to(b) > elsif b.can_compare_to?(a) > return b.compare_to(a) > else > return false > end > end How about def compare(a, b) retval = false if a.can_compare_to(b) retval = a.compare_to(b) end if retval and b.can_compare_to(a) retval = b.compare_to(a) end return retval end This way if both of them know how to compare themselves to the other one, the comparison returns true only if both agree they're equal. Aside from the code though, I'm not sure if I like the idea. While in principle I agree that equality tests should be symmetrical, I also think that it is more natural to have an equality operator which you can overload. I think you'd still need "can_compare_to?" to know if 'a' knows how to compare itself to an instance of 'b'... Maybe instead: def compare(a, b) retval = false if a.can_compare_to?(b) retval = a.==(b) end if retval and b.can_compare_to?(a) retval = b.==(a) end return retval end Interesting food for thought though. Ben