Sha256: 8e85f1f2fec684ae845adf8bb66e0b2964fb545504bbe345d1a0aad716d2ffca

Contents?: true

Size: 509 Bytes

Versions: 4

Compression:

Stored size: 509 Bytes

Contents

class String
  # In place word filter.
  #
  #   require 'facet/string/word_filter!'
  #
  #   "a string".word_filter! { |word| ... }
  #
  def word_filter! #:yield:
    rest_of_string = self
    wordfind = /(\w+)/
    offset = 0
    while wmatch = wordfind.match(rest_of_string)
      word = wmatch[0]
      range = offset+wmatch.begin(0) ... offset+wmatch.end(0)
      rest_of_string = wmatch.post_match
      self[range] = yield( word )
      offset = self.length - rest_of_string.length
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
facets-0.6.3 lib/facet/string/word_filter!.rb
facets-0.7.0 lib/facet/string/word_filter.rb
facets-0.7.1 lib/facet/string/word_filter.rb
facets-0.7.2 lib/facet/string/word_filter.rb