Bug #11422
closedAll common set operations produce incorrect (and surprising) results on a set of sets
Description
Set operations on a set of sets produce incorrect results¶
It seems that almost all common set operations (subtract, superset?, subset?, difference, etc...)
produce (mathematically) incorrect results on a set of sets, in that they just return the set of sets unmodified.
The union, intersection, and exclusion operations produce even weirder results, even when
both parameters are sets of sets – some members of the result are scalar values and some are sets.
2.2.2 :119 > ab = Set.new(['A', 'B'])
=> #<Set: {"A", "B"}>
2.2.2 :120 > c = Set.new(['C'])
=> #<Set: {"C"}>
2.2.2 :121 > abc = Set.new([ab, c])
Substraction fails¶
2.2.2 :122 > abc - c
=> #<Set: {#<Set: {"A", "B"}>, #<Set: {"C"}>}>
Superset fails¶
2.2.2 :140 > abc > ab
=> false
See more examples on pastie.org.
Python just throws TypeError: unhashable type: 'set'
.
Updated by peterhil (Peter Hillerström) over 9 years ago
Mon_Ouie pointed out at freenode #Ruby irc channel, that I have different number of levels that I compare.
This works:
pry(main):9> abc > Set.new([c])
=> true
pry(main):10> abc - Set.new([c])
=> #<Set: {#<Set: {"a", "b"}>}>
A shorter syntax like in Python would make this mistake harder to do, but then on the other hand people would accidentally make sets, when they wanted to make hashes:
{:a, :b, :c} > {:c}
=> true
Updated by marcandre (Marc-Andre Lafortune) over 9 years ago
- Status changed from Open to Rejected
This is as per spec, a set of sets is not the same as the union of those sets.
You may want to use abc = ab + c
instead of abc = Set.new([ab, c])
?