module StringGsub def sub(pattern, replace=nil, &block) return super if pattern.is_a? String out = "" s = self x = pattern.match(s) if x out << x.pre_match if replace out << replace else block.call(x.match(0)) end s = x.post_match end out << s out end def gsub(pattern, replace=nil, &block) return super if pattern.is_a? String out = "" s = self while (x = pattern.match(s)) out << x.pre_match if replace out << replace else out << block.call(x[0]) end s = x.post_match end out << s out end end class String prepend StringGsub end