## Feature #6588

### Set#intersect?

**Description**

There is `Set#superset?`

, `Set#subset?`

with their `proper`

variants, but there is no `Set#intersect?`

nor `Set#disjoint?`

To check if two sets intersect, one can do

set.intersection(other).empty?

This cycles through all elements, though. To be efficient, one can instead do the iteration manually:

other.any? { |x| set.include?(x) }

I think it would be natural to add `Set#intersect?`

and its reverse `Set#disjoint?`

class Set

def intersect?(enum)

enum.any? { |x| include?(x) }

end

end

Maybe it would be worth it to optimize it if enum is a larger Set by starting it with

return any? { |x| enum.include?(x) } if enum.is_a?(Set) && enum.size > size

### Associated revisions

Add Set#intersect? and #disjoint?.

- lib/set.rb (Set#intersect?, Set#disjoint?): Add new methods for testing if two sets have any element in common. [Feature #6588] Based on the code by marcandre.

Add Set#intersect? and #disjoint?.

- lib/set.rb (Set#intersect?, Set#disjoint?): Add new methods for testing if two sets have any element in common. [Feature #6588] Based on the code by marcandre.

Add Set#intersect? and #disjoint?.

- lib/set.rb (Set#intersect?, Set#disjoint?): Add new methods for testing if two sets have any element in common. [Feature #6588] Based on the code by marcandre.

Add Set#intersect? and #disjoint?.

Add Set#intersect? and #disjoint?.

### History

#### #1 [ruby-core:46459] Updated by mame (Yusuke Endoh) about 6 years ago

**Status**changed from*Open*to*Assigned*

#### #2 [ruby-core:48259] Updated by yhara (Yutaka HARA) almost 6 years ago

**Target version**changed from*2.0.0*to*2.6*

#### #3 [ruby-core:49242] Updated by marcandre (Marc-Andre Lafortune) almost 6 years ago

Comment about these simple features would be appreciated.

#### #4 [ruby-core:49252] Updated by alexeymuranov (Alexey Muranov) almost 6 years ago

+1. Maybe `#meet?`

instead of `#intersect?`

? It can be argued that any set intersects any other, just the intersection is sometimes empty :).

#### #5 [ruby-core:49258] Updated by marcandre (Marc-Andre Lafortune) almost 6 years ago

alexeymuranov (Alexey Muranov) wrote:

+1.

Thanks for the +1

It can be argued that any set intersects any other, just the intersection is sometimes empty :).

No, I believe it would be wrong to argue that. From wikipedia: "We say that A intersects B if A intersects B at some element"

Moreover: "We say that A and B are disjoint if A does not intersect B. In plain language, they have no elements in common"

I believe that both `intersect?`

and `disjoint?`

are the established terms for the concept I'm proposing.

#### #6 [ruby-core:56217] Updated by knu (Akinori MUSHA) about 5 years ago

OK, accepted. I'll work on it.

#### #7 Updated by knu (Akinori MUSHA) about 5 years ago

**Status**changed from*Assigned*to*Closed***% Done**changed from*0*to*100*

This issue was solved with changeset r42253.

Marc-Andre, thank you for reporting this issue.

Your contribution to Ruby is greatly appreciated.

May Ruby be with you.

Add Set#intersect? and #disjoint?.

#### #8 [ruby-core:56268] Updated by knu (Akinori MUSHA) about 5 years ago

I followed superset?() and the like, and made the new methods accept only a set for the moment, because I couldn't come up with an idea of how to deal with Range. For example:

- if Set[2].intersect?(1.5..2.5) should return true
- if Set[2].intersect?(3..(1.0/0)) should immediately return false using some knowledge on Range

Add Set#intersect? and #disjoint?.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@42253 b2dd03c8-39d4-4d8f-98ff-823fe69b080e