Method: Set#&

Defined in:
lib/set.rb

#&(enum) ⇒ Object Also known as: intersection

Returns a new set containing elements common to the set and the given enumerable object.

Set[1, 3, 5] & Set[3, 2, 1]             #=> #<Set: {3, 1}>
Set['a', 'b', 'z'] & ['a', 'b', 'c']    #=> #<Set: {"a", "b"}>
[View source]

481
482
483
484
485
486
487
488
489
490
491
492
493
# File 'lib/set.rb', line 481

def &(enum)
  n = self.class.new
  if enum.is_a?(Set)
    if enum.size > size
      each { |o| n.add(o) if enum.include?(o) }
    else
      enum.each { |o| n.add(o) if include?(o) }
    end
  else
    do_with_enum(enum) { |o| n.add(o) if include?(o) }
  end
  n
end