Created
October 7, 2010 11:45
-
-
Save banister/614991 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
module Recipe | |
def eggs(num=nil) | |
num ? @eggs = num : @eggs | |
end | |
def vinegar(num=nil) | |
num ? @vinegar = num : @vinegar | |
end | |
end | |
def recipe(&block) | |
context = eval('self', block.binding) | |
context_copy = context.dup | |
context_copy.extend(Recipe) | |
context_copy.instance_eval(&block) | |
# update state in the original | |
context_copy.instance_variables.each do |v| | |
context.instance_variable_set(v, context_copy.instance_variable_get(v)) | |
end | |
[context.instance_variable_get(:@eggs), context.instance_variable_get(:@vinegar)] | |
end | |
class MyRecipes | |
def self.calc_eggs | |
99 | |
end | |
@vinegar_count = 8 | |
r = recipe do | |
eggs calc_eggs | |
vinegar @vinegar_count | |
end | |
p r | |
# check to see that eggs method is no longer mixed in | |
eggs rescue puts "eggs method no longer exists (existed only in copy" | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment