From: "knu (Akinori MUSHA)" Date: 2013-07-30T19:19:21+09:00 Subject: [ruby-core:56268] [ruby-trunk - Feature #6588] Set#intersect? Issue #6588 has been updated by knu (Akinori MUSHA). 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 ---------------------------------------- Feature #6588: Set#intersect? https://bugs.ruby-lang.org/issues/6588#change-40759 Author: marcandre (Marc-Andre Lafortune) Status: Closed Priority: Normal Assignee: knu (Akinori MUSHA) Category: lib Target version: next minor 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 -- http://bugs.ruby-lang.org/