Skip to content

Instantly share code, notes, and snippets.

@dv
Created September 21, 2011 23:31
Show Gist options
  • Save dv/1233615 to your computer and use it in GitHub Desktop.
Save dv/1233615 to your computer and use it in GitHub Desktop.
Attach local variables to ERB.
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