Module: Util

Defined in:
lib/util.rb

Overview

require ‘util’

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.mgsub(string, substitution_hash) ⇒ Object

Multiple Global Substitution.

substitution_hash contains pattern (regexp or string) keys and corresponding substitution. Substitution can either be a string or a function of regexp MatchData. The latter allows for arbitrary substitution based on matched content.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/util.rb', line 18

def self.mgsub(string, substitution_hash)
  regexp_hash = {}

  substitution_hash.each do |key, substitution|
    regexp = to_regexp(key)
    regexp_hash[regexp] = substitution
  end

  combined_regexp = Regexp.union(*regexp_hash.keys)
  
  substitution_function = make_substitution_function(regexp_hash)

  string.gsub(combined_regexp, &substitution_function)
end

Instance Method Details

#mgsub(string, substitution_hash) ⇒ Object

Sugar to allow calling mgsub as instance method.



7
8
9
# File 'lib/util.rb', line 7

def mgsub(string, substitution_hash)
  Util::mgsub(string, substitution_hash)
end