Created
February 12, 2015 18:25
-
-
Save hawx/d15617942758e8f54f8f to your computer and use it in GitHub Desktop.
'set' local variables within a proc before it runs
This file contains hidden or 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
# @param args [Hash{Symbol=>Object}] | |
def run_proc_with_locals(args, proc) | |
k = Class.new | |
args.each do |sym, v| | |
k.send(:define_method, sym) { v } | |
end | |
k.new.instance_exec(&proc) | |
end | |
tha_proc = proc do | |
puts "Hello #{name}" | |
end | |
run_proc_with_locals({:name => "world"}, tha_proc) | |
#=> Hello world |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment