Last active
February 4, 2016 19:47
-
-
Save bbuck/b69fe328fe1daced1857 to your computer and use it in GitHub Desktop.
Ruby ERB String Extensions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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