Sha256: 1b8c07e716b6f157a11e0425d62dacc331462717dc71de749bb6632f7b9dd4e4

Contents?: true

Size: 1.2 KB

Versions: 1

Compression:

Stored size: 1.2 KB

Contents

# frozen_string_literal: true
require_relative 'binary_operator'

module Wordlist
  module Operators
    #
    # Lazily enumerates over the combination of the words from two wordlists.
    #
    # @since 1.0.0
    #
    class Product < BinaryOperator

      #
      # Enumerates over every combination of the words from the two wordlist.
      #
      # @yield [string]
      #   The given block will be passed each combination of words from the
      #   two wordlist.
      #
      # @yieldparam [String] string
      #   A combination of two words from the two wordlists.
      #
      # @return [Enumerator]
      #   If no block is given, an Enumerator object will be returned.
      #
      # @example
      #   wordlist1 = Wordlist::Words["foo", "bar"]
      #   wordlist2 = Wordlist::Words["ABC", "XYZ"]
      #   (wordlist1 * wordlist2).each do |word|
      #     puts word
      #   end
      #   # fooABC
      #   # fooXYZ
      #   # barABC
      #   # barXYZ
      #
      # @api public
      #
      def each
        return enum_for(__method__) unless block_given?

        @left.each do |word1|
          @right.each do |word2|
            yield word1 + word2
          end
        end
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
wordlist-1.1.1 lib/wordlist/operators/product.rb