diff options
author | knu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2013-07-30 09:58:13 +0000 |
---|---|---|
committer | knu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2013-07-30 09:58:13 +0000 |
commit | bd304ed85bc2f3c0cc57334fe624fc0efa122cbc (patch) | |
tree | b71bc1e56607fa98e3c77f3c7575cc5e888de376 /lib/set.rb | |
parent | 2e0fc4b21e6c14565afea8eb8c4d38097ead77ee (diff) |
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.
[ruby-core:45641] [Feature #6588] Based on the code by marcandre.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@42253 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/set.rb')
-rw-r--r-- | lib/set.rb | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/lib/set.rb b/lib/set.rb index 47fcc3efc7..aec22ef4fe 100644 --- a/lib/set.rb +++ b/lib/set.rb @@ -231,6 +231,23 @@ class Set end alias < proper_subset? + # Returns true if the set and the given set have at least one + # element in common. + def intersect?(set) + set.is_a?(Set) or raise ArgumentError, "value must be a set" + if size < set.size + any? { |o| set.include?(o) } + else + set.any? { |o| include?(o) } + end + end + + # Returns true if the set and the given set have no element in + # common. This method is the opposite of +intersect?+. + def disjoint?(set) + !intersect?(set) + end + # Calls the given block once for each element in the set, passing # the element as parameter. Returns an enumerator if no block is # given. |