[Ruby] eql? and hash on Sets

Jim Ludwig jsl at blarg.net
Sun Nov 25 17:42:16 PST 2007


John Barnette wrote:
>> Given two sets, (first - second).empty? is
>> probably going to be the best way to test for
>> equality.

If one already knows the two sets are the same
size, then the above is true.  Otherwise one would
need to subtract in both directions in order to
determine equality:

  (first - second).empty? && (second - first).empty?

I kind of lost track whether the original question
had to do with looking for a decent way to test
Set equality or with wondering why certain Set
equality methods were implemented the way they
were.

Assuming the former, set equality is often defined
as: A == B iff A is a subset of B and B is a
subset of A.  So why not just go with that?:

  first.subset?(second) && second.subset?(first)

Cheers,
  jim


More information about the Ruby mailing list