Method: Weak::Set#intersect?

Defined in:
lib/weak/set.rb

#intersect?(enum) ⇒ Bool

Note:

Weak::Set does not test member equality with == or eql?. Instead, it always checks strict object equality, so that, e.g., different strings are not considered equal, even if they may contain the same string content.

Returns true if self and the given enumerable object have at least one element in common, false otherwise.

Examples:

Weak::Set[1, 2, 3].intersect? Weak::Set[4, 5]   #=> false
Weak::Set[1, 2, 3].intersect? Weak::Set[3, 4]   #=> true
Weak::Set[1, 2, 3].intersect? 4..5              #=> false
Weak::Set[1, 2, 3].intersect? [3, 4]            #=> true

Parameters:

  • a Weak::Set, or an Enumerable object, e.g. an Array or Set, or an object which responds to each_entry or each

Returns:

  • true if self and the given enumerable object have at least one element in common, false otherwise



504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
# File 'lib/weak/set.rb', line 504

def intersect?(enum)
  case enum
  when Weak::Set
    enum_ary = enum.to_a
    own_ary = to_a

    if own_ary.size < enum_ary.size
      own_ary.any?(enum)
    else
      enum_ary.any?(self)
    end
  else
    enumerable(enum).any?(self)
  end
end