Skip to content

Instantly share code, notes, and snippets.

@zzak
Forked from nusco/deferred_evaluation.rb
Created September 13, 2010 16:54
Show Gist options
  • Save zzak/577609 to your computer and use it in GitHub Desktop.
Save zzak/577609 to your computer and use it in GitHub Desktop.
# ==========================
# 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