From: Christoph Rippel Date: 2000-12-04T07:22:16+09:00 Subject: [ruby-talk:6786] Re: Array Intersect (&) question Hi, some of the examples in this thread are misleading since the elements of the are always comparable. What should be a natural order of [Array,"a",13] & ["a",13,Array] -> ?. Here is a naive (but semantically correct) implementation of ``Set'' together with a conversion method class Hash # as Set def &(other) set={} each_key do |i| set[i]= 1 if other.member?(i) end set end # implement other set operations end class Array def to_set set = {} each do |i| set[i] = 1 end set end end To say that the Array intersection is compatible with a mathematical set theoretic intersection boils down to an equality of (a & b).to_set == a.to_set & b.to_set for all Array objects a,b. The order and multiplicity of a & b is of no relevance at all. If one wants to achieve compatibility with the mathematical multi set interpretation (essentially Hashes with values in [1,2,3 .. ] ) the same equations must hold with to_set replaced by to_mset. Note this time multiplicity matters but the order is still irrelevant. class Hash # as Multiset def &(other) set={} each_key do |i| set[i]= min(self[i],other[i]) if other[i] end set end # implement other multi set methods private def min(n,m) (n