Sha256: c50dc7915b73f7fa991768da7fd98094758932edcba0bd626a6fd04bc9ead716

Contents?: true

Size: 702 Bytes

Versions: 6

Compression:

Stored size: 702 Bytes

Contents

class String

  # Apply a set of rules (regular expression matches) to the
  # string.
  #
  # === Requirements:
  # The rules must be applied in order! So we cannot use a
  # hash because the ordering is not guaranteed! we use an
  # array instead.
  #
  # === Input:
  # The array containing rule-pairs (match, write).
  #
  # === Output:
  # The rewritten string.

  def rewrite(string, rules)
    return nil unless string

    # gmosx: helps to find bugs
    raise ArgumentError.new('The rules parameter is nil') unless rules

    rewritten_string = string.dup

    rules.each do |match,write|
      rewritten_string.gsub!(match,write)
    end

    return (rewritten_string or string)
  end

end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
facets-1.8.49 lib/facets/core/string/rewrite.rb
facets-1.8.0 lib/facets/core/string/rewrite.rb
facets-1.8.20 lib/facets/core/string/rewrite.rb
facets-1.8.51 lib/facets/core/string/rewrite.rb
facets-1.8.54 lib/facets/core/string/rewrite.rb
facets-1.8.8 lib/facets/core/string/rewrite.rb