Class: Wordlist::Operators::Subtract

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

Overview

Lazily enumerates over every word in the first wordlist, that is not in the second wordlist.

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 difference between the two wordlists.

Examples:

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

Yields:

  • (word)

    The given block will be passed each word from the first wordlist, that is not in the second wordlist.

Yield Parameters:

  • word (String)

    A word that belongs to first wordlist, but not the second wordlist.

Returns:

  • (Enumerator)

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

Since:

  • 1.0.0



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

def each
  return enum_for(__method__) unless block_given?

  unique_filter = UniqueFilter.new

  @right.each { |word| unique_filter.add(word) }

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