Skip to content

Instantly share code, notes, and snippets.

@stemar
Last active September 15, 2019 04:31
Show Gist options
  • Select an option

  • Save stemar/a9b038b53005cf00922dc0455c97047a to your computer and use it in GitHub Desktop.

Select an option

Save stemar/a9b038b53005cf00922dc0455c97047a to your computer and use it in GitHub Desktop.
Replace a string's placeholders with the provided key/value pairs
class String
def p(**args)
gsub(/:[\w]*/){|match| args[match[1..-1].to_sym] || match}
end
end
"This :code contains :placeholders".p(:code=>"sentence", :placeholders=>"strings")
# => "This sentence contains strings"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment