-
-
Save zzak/577609 to your computer and use it in GitHub Desktop.
This file contains 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
# ========================== | |
# Spell: Deferred Evaluation | |
# ========================== | |
# Store a piece of code and its context in a proc or lambda for evaluation later. | |
class C | |
def store(&block) | |
@my_code_capsule = block | |
end | |
def execute | |
@my_code_capsule.call | |
end | |
end | |
obj = C.new | |
obj.store { $X = 1 } | |
$X = 0 | |
obj.execute | |
$X # => 1 | |
# For more information: http://www.pragprog.com/titles/ppmetr/metaprogramming-ruby |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment