I have a block. I want to get a binding which contains all the variables in that blocks scope at the end of running the block and then take that binding and pass it to ERB.new("").result. block.binding doesn't seem to work, and nothing else I try seems to. Tips?
I have a question about the X (the why) of the Y (what you say you want to do). Can you give us some context about it? When you say "variables"?, what do you mean?
It's in a gem I'm working on, and the user inputs a block, which should provide variables for an ERB compilation(?). The ERB compilation occurs in a different scope from the initial scope of the block, and I don't want to pollute that scope with the variables from the block by calling it. Also, the result instance method for an ERB object takes a binding, so it seems like that is how I should do it.
The problem is where the binding is being executed. The 1.8.7-way obj.send(:binding) does not work anymore (see issue2161), the environment must be the object itself. So use instance_eval:
require 'ostruct'
require 'erb'
namespace = OpenStruct.new(:first => 'Salvador', :last => 'Espriu')
templa...
@thesecretmaster ^ This is way better than how I did it.