Class: Wordlist::Modifiers::Tr

Inherits:
Modifier
  • Object
show all
Defined in:
lib/wordlist/modifiers/tr.rb

Overview

Lazily calls String#tr on every word in the wordlist.

Since:

  • 1.0.0

Instance Attribute Summary collapse

Attributes inherited from Modifier

#wordlist

Instance Method Summary collapse

Constructor Details

#initialize(wordlist, chars, replace) ⇒ Tr

Initializes the String#tr modifier.

Parameters:

  • chars (String)

    The characters or character range to replace.

  • replace (String)

    The characters or character range to use as the replacement.

Since:

  • 1.0.0



32
33
34
35
36
37
# File 'lib/wordlist/modifiers/tr.rb', line 32

def initialize(wordlist,chars,replace)
  super(wordlist)

  @chars   = chars
  @replace = replace
end

Instance Attribute Details

#charsString (readonly)

The characters or character range to translate.

Returns:

  • (String)

Since:

  • 1.0.0



16
17
18
# File 'lib/wordlist/modifiers/tr.rb', line 16

def chars
  @chars
end

#replaceString (readonly)

The characters or character range to translate to.

Returns:

  • (String)

Since:

  • 1.0.0



21
22
23
# File 'lib/wordlist/modifiers/tr.rb', line 21

def replace
  @replace
end

Instance Method Details

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

Enumerates over every tred word in the wordlist.

Examples:

wordlist = Wordlist::Words["foo", "bar", "baz"]
wordlist.tr("oa", "0@").each do |word|
  puts word
end
# f00
# b@r
# b@z

Yields:

  • (word)

    The given block will be passed each tred word.

Yield Parameters:

  • word (String)

    A tred word.

Returns:

  • (Enumerator)

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

Since:

  • 1.0.0



62
63
64
65
66
67
68
# File 'lib/wordlist/modifiers/tr.rb', line 62

def each
  return enum_for(__method__) unless block_given?

  @wordlist.each do |word|
    yield word.tr(@chars,@replace)
  end
end