Created
September 21, 2011 23:31
-
-
Save dv/1233615 to your computer and use it in GitHub Desktop.
Attach local variables to ERB.
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" | |
require "ostruct" | |
vars = OpenStruct.new( :name => "David", :state => "Awesome" ) | |
# Either update OpenStruct with get_binding() | |
class OpenStruct | |
def get_binding | |
return binding() | |
end | |
end | |
binding = vars.get_binding | |
# Or use instance_eval | |
binding = vars.instance_eval("binding") | |
# This does not work | |
# binding = vars.send(:binding) | |
result = ERB.new("Hello <%= name %>, you're <%= state %>!").result(binding) | |
# "Hello David, you're awesome!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment