Class: Wordlist::Modifiers::Mutate

Inherits:
Sub show all
Defined in:
lib/wordlist/modifiers/mutate.rb

Overview

Lazily enumerates through every combination of a string substitution on every word in the wordlist.

Since:

  • 1.0.0

Direct Known Subclasses

MutateCase

Instance Attribute Summary

Attributes inherited from Sub

#block, #pattern, #replace

Attributes inherited from Modifier

#wordlist

Instance Method Summary collapse

Methods inherited from Sub

#initialize

Methods inherited from Modifier

#initialize

Constructor Details

This class inherits a constructor from Wordlist::Modifiers::Sub

Instance Method Details

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

Enumerates over every mutation of every word in the wordlist.

Examples:

wordlist = Wordlist::Words["foo", "bar", "baz"]
wordlist.mutate(/[oa]/, {'o' => '0', 'a' => '@'}).each do |word|
  puts word
end
# foo
# f0o
# fo0
# f00
# bar
# b@r
# baz
# b@z

Yields:

  • (word)

    The given block will be passed each mutation of each word.

Yield Parameters:

  • word (String)

    A mutated word.

Returns:

  • (Enumerator)

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

Since:

  • 1.0.0



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/wordlist/modifiers/mutate.rb', line 42

def each
  return enum_for(__method__) unless block_given?

  @wordlist.each do |word|
    yield word

    matches = all_matches(word)

    each_combination(matches) do |selected_matches|
      new_word = word.dup
      offset   = 0

      selected_matches.each do |match|
        index, end_index = match.offset(0)
        length = end_index - index

        matched_string = match[0]
        replace_string = substitute(matched_string)

        new_word[index+offset,length] = replace_string

        offset += (replace_string.length - length)
      end

      yield new_word
    end
  end
end