[ruby/set] Allow Set#intersect? and #disjoint? to accept array argument
Implements [Feature #17838] https://github.com/ruby/set/commit/d9b389bafa
This commit is contained in:
parent
27fb9d272d
commit
571dafdc7f
@ -474,7 +474,14 @@ class Set
|
|||||||
# Set[1, 2, 3].intersect? Set[4, 5] #=> false
|
# Set[1, 2, 3].intersect? Set[4, 5] #=> false
|
||||||
# Set[1, 2, 3].intersect? Set[3, 4] #=> true
|
# Set[1, 2, 3].intersect? Set[3, 4] #=> true
|
||||||
def intersect?(set)
|
def intersect?(set)
|
||||||
set.is_a?(Set) or raise ArgumentError, "value must be a set"
|
case set
|
||||||
|
when Set
|
||||||
|
# nothing
|
||||||
|
when Array
|
||||||
|
Set.new(set)
|
||||||
|
else
|
||||||
|
raise ArgumentError, "value must be a set or array"
|
||||||
|
end
|
||||||
if size < set.size
|
if size < set.size
|
||||||
any? { |o| set.include?(o) }
|
any? { |o| set.include?(o) }
|
||||||
else
|
else
|
||||||
|
@ -354,13 +354,17 @@ class TC_Set < Test::Unit::TestCase
|
|||||||
case expected
|
case expected
|
||||||
when true
|
when true
|
||||||
assert_send([set, :intersect?, other])
|
assert_send([set, :intersect?, other])
|
||||||
|
assert_send([set, :intersect?, other.to_a])
|
||||||
assert_send([other, :intersect?, set])
|
assert_send([other, :intersect?, set])
|
||||||
assert_not_send([set, :disjoint?, other])
|
assert_not_send([set, :disjoint?, other])
|
||||||
|
assert_not_send([set, :disjoint?, other.to_a])
|
||||||
assert_not_send([other, :disjoint?, set])
|
assert_not_send([other, :disjoint?, set])
|
||||||
when false
|
when false
|
||||||
assert_not_send([set, :intersect?, other])
|
assert_not_send([set, :intersect?, other])
|
||||||
|
assert_not_send([set, :intersect?, other.to_a])
|
||||||
assert_not_send([other, :intersect?, set])
|
assert_not_send([other, :intersect?, set])
|
||||||
assert_send([set, :disjoint?, other])
|
assert_send([set, :disjoint?, other])
|
||||||
|
assert_send([set, :disjoint?, other.to_a])
|
||||||
assert_send([other, :disjoint?, set])
|
assert_send([other, :disjoint?, set])
|
||||||
when Class
|
when Class
|
||||||
assert_raise(expected) {
|
assert_raise(expected) {
|
||||||
@ -378,7 +382,7 @@ class TC_Set < Test::Unit::TestCase
|
|||||||
set = Set[3,4,5]
|
set = Set[3,4,5]
|
||||||
|
|
||||||
assert_intersect(ArgumentError, set, 3)
|
assert_intersect(ArgumentError, set, 3)
|
||||||
assert_intersect(ArgumentError, set, [2,4,6])
|
assert_intersect(true, set, Set[2,4,6])
|
||||||
|
|
||||||
assert_intersect(true, set, set)
|
assert_intersect(true, set, set)
|
||||||
assert_intersect(true, set, Set[2,4])
|
assert_intersect(true, set, Set[2,4])
|
||||||
|
Loading…
x
Reference in New Issue
Block a user