Class: Wordlist::Operators::Power

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

Overview

Lazily enumerates over every combination of words in the wordlist.

Since:

  • 1.0.0

Instance Attribute Summary collapse

Attributes inherited from BinaryOperator

#left, #right

Instance Method Summary collapse

Constructor Details

#initialize(wordlist, exponent) ⇒ Power

Initializes the power operator.

Parameters:

  • wordlist (Enumerable)
  • exponent (Integer)

Since:

  • 1.0.0



28
29
30
31
32
33
34
35
36
# File 'lib/wordlist/operators/power.rb', line 28

def initialize(wordlist,exponent)
  super(wordlist,exponent)

  @wordlists = wordlist

  (exponent - 1).times do
    @wordlists = Product.new(wordlist,@wordlists)
  end
end

Instance Attribute Details

#wordlistsProduct (readonly)

The product of the wordlist with itself.

Returns:

Since:

  • 1.0.0



17
18
19
# File 'lib/wordlist/operators/power.rb', line 17

def wordlists
  @wordlists
end

Instance Method Details

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

Enumerates over every combination of words from the wordlist.

Examples:

wordlist = Wordlist::Words["foo", "bar"]
(wordlist ** 3).each do |word|
  puts word
end
# foofoofoo
# foofoobar
# foobarfoo
# foobarbar
# barfoofoo
# barfoobar
# barbarfoo
# barbarbar

Yields:

  • (string)

    The given block will be passed each combination of words from the wordlist.

Yield Parameters:

  • string (String)

    A combination of words from the wordlist.

Returns:

  • (Enumerator)

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

Since:

  • 1.0.0



67
68
69
# File 'lib/wordlist/operators/power.rb', line 67

def each(&block)
  @wordlists.each(&block)
end