Class: Wordlist::Modifiers::Sub
- Defined in:
- lib/wordlist/modifiers/sub.rb
Overview
Lazily calls String#sub
on every word in the wordlist.
Instance Attribute Summary collapse
-
#block ⇒ Proc?
readonly
The optional block to call when replacing matched substrings.
-
#pattern ⇒ Regexp, String
readonly
The pattern to substitute.
-
#replace ⇒ String, Hash{String => String, nil}
readonly
The replacement String or map of Strings.
Attributes inherited from Modifier
Instance Method Summary collapse
-
#each {|word| ... } ⇒ Enumerator
Enumerates over every
sub
ed word in the wordlist. -
#initialize(wordlist, pattern, replace = nil) {|match| ... } ⇒ Sub
constructor
Initializes the
String#sub
modifier.
Constructor Details
#initialize(wordlist, pattern, replace = nil) {|match| ... } ⇒ Sub
Initializes the String#sub
modifier.
47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/wordlist/modifiers/sub.rb', line 47 def initialize(wordlist,pattern,replace=nil,&block) super(wordlist) @pattern = pattern @replace = case replace when String, Hash, nil then replace else raise(TypeError,"no implicit conversion of #{replace.class} to String") end @block = block end |
Instance Attribute Details
#block ⇒ Proc? (readonly)
The optional block to call when replacing matched substrings.
26 27 28 |
# File 'lib/wordlist/modifiers/sub.rb', line 26 def block @block end |
#pattern ⇒ Regexp, String (readonly)
The pattern to substitute.
16 17 18 |
# File 'lib/wordlist/modifiers/sub.rb', line 16 def pattern @pattern end |
#replace ⇒ String, Hash{String => String, nil} (readonly)
The replacement String or map of Strings.
21 22 23 |
# File 'lib/wordlist/modifiers/sub.rb', line 21 def replace @replace end |
Instance Method Details
#each {|word| ... } ⇒ Enumerator
Enumerates over every sub
ed word in the wordlist.
82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/wordlist/modifiers/sub.rb', line 82 def each return enum_for(__method__) unless block_given? if @replace @wordlist.each do |word| yield word.sub(@pattern,@replace) end else @wordlist.each do |word| yield word.sub(@pattern,&block) end end end |