Skip to content

Instantly share code, notes, and snippets.

@yannlugrin
Created March 11, 2009 13:28
Show Gist options
  • Save yannlugrin/77466 to your computer and use it in GitHub Desktop.
Save yannlugrin/77466 to your computer and use it in GitHub Desktop.
def multi_replace(string, *rules)
scanning_pattern = Regexp.union(rules.map{|e| e.first} | [/./])
string.scan(scanning_pattern).map do |match|
rules.select{|e| match =~ e.first}.flatten.last || match
end.join
end
multi_replace("the first problem is not egal to second problem", [/first problem/, "older problem"], [/second problem/, "first problem"])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment