Module: Util
- Defined in:
- lib/util.rb
Overview
require ‘util’
Class Method Summary collapse
-
.mgsub(string, substitution_hash) ⇒ Object
Multiple Global Substitution.
Instance Method Summary collapse
-
#mgsub(string, substitution_hash) ⇒ Object
Sugar to allow calling mgsub as instance method.
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 |