From: Gavin Kistner Date: 2006-11-02T05:32:11+09:00 Subject: Re: Question about the Set class, XOR, and arrays From: Daniel Berger [mailto:djberg96@gmail.com] > irb(main):003:0> Set[1,2,3] ^ [3,4,5,5] > => # > > What?! I'm confused. Do I need a refresher in Set theory or something? Looks like a bug due to Set#^'s implementation that assumes that the enumerable passed in has each item only once (or, due to the implementation, an odd number of times). irb(main):001:0> require 'set' => true irb(main):002:0> a = Set[1,2,3] => # irb(main):003:0> a ^ [3,4,5] => # irb(main):004:0> a ^ [3,4,5,5] => # irb(main):005:0> a ^ [3,4,5,5,5] => # The first time it sees "5", it adds it to the resulting set. The second time it sees 5, it says "Oh, I already have a 5 in the result, better remove it." def ^(enum) enum.is_a?(Enumerable) or raise ArgumentError, "value must be enumerable" n = dup enum.each { |o| if n.include?(o) then n.delete(o) else n.add(o) end } n end Use #uniq on the array to get a proper answer: irb(main):006:0> a ^ [3,4,5,5].uniq => #