Created
August 8, 2011 18:20
-
-
Save Mon-Ouie/1132344 to your computer and use it in GitHub Desktop.
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
| 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