Skip to content

Instantly share code, notes, and snippets.

@bbuck
Last active February 4, 2016 19:47
Show Gist options
  • Save bbuck/b69fe328fe1daced1857 to your computer and use it in GitHub Desktop.
Save bbuck/b69fe328fe1daced1857 to your computer and use it in GitHub Desktop.
Ruby ERB String Extensions
require 'erb'
class ERBContext
def initialize(hash)
hash.each_pair do |key, value|
singleton_class.send(:define_method, key) { value }
end
end
def get_binding
binding
end
end
class ::String
def erb(assigns = {})
ERB.new(self).result(ERBContext.new(assigns).get_binding)
end
end
p "Hello, <%= name %>!".erb(name: "World") # => "Hello, World!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment