Skip to content

Instantly share code, notes, and snippets.

@hawx
Created February 12, 2015 18:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hawx/d15617942758e8f54f8f to your computer and use it in GitHub Desktop.
Save hawx/d15617942758e8f54f8f to your computer and use it in GitHub Desktop.
'set' local variables within a proc before it runs
# @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