From: noreply@... Date: 2006-11-02T06:24:40+09:00 Subject: [ ruby-Bugs-6444 ] Set#^ (xor) is broken - patch included Bugs item #6444, was opened at 2006-11-01 14:24 You can respond by visiting: http://rubyforge.org/tracker/?func=detail&atid=1698&aid=6444&group_id=426 Category: Standard Library Group: 1.8.x Status: Open Resolution: None Priority: 3 Submitted By: Daniel Berger (djberg96) Assigned to: Nobody (None) Summary: Set#^ (xor) is broken - patch included Initial Comment: The current Set#^ method is broken: irb(main):002:0> Set[1,2,3] ^ Set[3,4,5,5] => # irb(main):003:0> Set[1,2,3] ^ [3,4,5,5] => # Subsequent duplicate values in the argument will cause it to toggle due to the current implementation. See ruby-talk:223176 and following. There are several ways this could be patched - I chose the path of least effort. In addition, I included a small test to verify that the patch works properly. All tests pass. --- set.orig Wed Nov 1 14:07:22 2006 +++ set.rb Wed Nov 1 14:09:22 2006 @@ -301,6 +301,7 @@ # ((set | enum) - (set & enum)). def ^(enum) enum.is_a?(Enumerable) or raise ArgumentError, "value must be enumerable" + enum = Set.new(enum) unless enum.is_a?(Set) n = dup enum.each { |o| if n.include?(o) then n.delete(o) else n.add(o) end } n @@ -1049,6 +1050,13 @@ assert_equal(Set[2,4], ret) end + def test_xor + set = Set[1,2,3,4] + ret = set ^ [2,4,5,5] + assert_not_same(set, ret) + assert_equal(Set[1,3,5], ret) + end + def test_eq set1 = Set[2,3,1] set2 = Set[1,2,3] ---------------------------------------------------------------------- You can respond by visiting: http://rubyforge.org/tracker/?func=detail&atid=1698&aid=6444&group_id=426