Class: Wordlist::Operators::Intersect

Inherits:
BinaryOperator show all
Defined in:
lib/wordlist/operators/intersect.rb

Overview

Lazily enumerates over every word that belongs to both wordlists.

Since:

  • 1.0.0

Instance Attribute Summary

Attributes inherited from BinaryOperator

#left, #right

Instance Method Summary collapse

Methods inherited from BinaryOperator

#initialize

Constructor Details

This class inherits a constructor from Wordlist::Operators::BinaryOperator

Instance Method Details

#each {|word| ... } ⇒ Enumerator

Enumerates over the intersection between two wordlists.

Examples:

wordlist1 = Wordlist::Words["foo", "bar", "baz", "qux"]
wordlist2 = Wordlist::Words["xyz", "bar", "abc", "qux"]
(wordlist1 & wordlist2).each do |word|
  puts word
end
# bar
# qux

Yields:

  • (word)

    The given block will be passed each word from the intersection between the two wordlists.

Yield Parameters:

  • word (String)

    A word that belongs to both wordlists.

Returns:

  • (Enumerator)

    If no block is given, an Enumerator object will be returned.

Since:

  • 1.0.0



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/wordlist/operators/intersect.rb', line 38

def each
  return enum_for(__method__) unless block_given?

  unique_filter = UniqueFilter.new

  @left.each do |word|
    unique_filter.add(word)
  end

  @right.each do |word|
    if unique_filter.include?(word)
      yield word
    end
  end
end