Skip to content

Instantly share code, notes, and snippets.

@adambair
Last active August 29, 2015 13:57
Show Gist options
  • Save adambair/9489552 to your computer and use it in GitHub Desktop.
Save adambair/9489552 to your computer and use it in GitHub Desktop.
gsub replacement via hash
replace = {'{{HAPPY}}' => 'sad', '{{FUN}}' => 'lame', '{{BALLS}}' => 'rock'}
subject = "{{HAPPY}} {{FUN}} {{BALLS}} are amazing"
search = Regexp.new(replace.keys.map{|k,v| "(#{k})"}.join('|'))
parsed = subject.gsub(search, replace)
puts "old: #{subject}"
# => old: {{HAPPY}} {{FUN}} {{BALLS}} are amazing
puts "new: #{parsed}"
# => new: sad lame rock are amazing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment