Skip to content

Instantly share code, notes, and snippets.

@Mon-Ouie
Created August 8, 2011 18:20
Show Gist options
  • Select an option

  • Save Mon-Ouie/1132344 to your computer and use it in GitHub Desktop.

Select an option

Save Mon-Ouie/1132344 to your computer and use it in GitHub Desktop.
class Proc
def merge_binding!(other_binding)
other_binding.eval("local_variables").each do |var|
var_id = other_binding.eval(var.to_s).object_id
binding.eval("#{var} = ObjectSpace._id2ref(#{var_id})")
end
end
end
x = 3
some_proc = proc { puts x }
some_proc.call
puts x
def foo(block)
x = 10
block.merge_binding! binding
end
foo some_proc
some_proc.call # Outputs 10!
puts x # 10 too
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment