Class: Wordlist::Operators::Union

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

Overview

Lazily enumerates over words from both wordlists, filtering out any duplicates.

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 union of the 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
# foo
# bar
# baz
# qux
# xyz
# abc

Yields:

  • (word)

    The given block will be passed each word from both wordlists, without duplicates.

Yield Parameters:

  • word (String)

    A word that belongs to one of the wordlists.

Returns:

  • (Enumerator)

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

Since:

  • 1.0.0



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/wordlist/operators/union.rb', line 43

def each
  return enum_for(__method__) unless block_given?

  unique_filter = UniqueFilter.new

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

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