Gavin Kistner
11/1/2006 8:32:00 PM
From: Daniel Berger [mailto:djberg96@gmail.com]
> irb(main):003:0> Set[1,2,3] ^ [3,4,5,5]
> => #<Set: {1, 2, 4}>
>
> 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]
=> #<Set: {1, 2, 3}>
irb(main):003:0> a ^ [3,4,5]
=> #<Set: {5, 1, 2, 4}>
irb(main):004:0> a ^ [3,4,5,5]
=> #<Set: {1, 2, 4}>
irb(main):005:0> a ^ [3,4,5,5,5]
=> #<Set: {5, 1, 2, 4}>
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
=> #<Set: {5, 1, 2, 4}>