class Wordlist::Modifiers::Sub
Lazily calls ‘String#sub` on every word in the wordlist.
@since 1.0.0
Attributes
block[R]
The optional block to call when replacing matched substrings.
@return [Proc, nil]
pattern[R]
The pattern to substitute.
@return [Regexp, String]
replace[R]
The replacement String or map of Strings.
@return [String, Hash{String => String, nil}]
Public Class Methods
new(wordlist,pattern,replace=nil,&block)
click to toggle source
Initializes the ‘String#sub` modifier.
@param [Regexp, String] pattern
The pattern to replace.
@param [String, Hash, nil] replace
The characters or character range to use as the replacement.
@yield [match]
The given block will be call to replace the matched substring, if `replace` is nil.
@yieldparam [String] match
A matched substring.
@raise [TypeError]
The `replace` value was not a String, Hash, or `nil`.
Calls superclass method
Wordlist::Modifiers::Modifier::new
# 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
Public Instance Methods
each() { |sub| ... }
click to toggle source
Enumerates over every ‘sub`ed word in the wordlist.
@yield [word]
The given block will be passed each `sub`ed word.
@yieldparam [String] word
A `sub`ed word.
@return [Enumerator]
If no block is given, an Enumerator object will be returned.
@example
wordlist = Wordlist::Words["foo", "bar", "baz"] wordlist.sub(/o/, '0').each do |word| puts word end # f0o # bar # baz
@api public
# 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