Created
March 26, 2011 12:06
-
-
Save DanielVartanov/888224 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
# Step 1. Setting up environment | |
class Government | |
def power | |
"belongs to government" | |
end | |
end | |
module People | |
def power | |
"belogs to people" | |
end | |
Government.__send__ :include, self | |
end | |
government = Government.new | |
# Step 2. Swapping ancestors by re-setting @superclass | |
included_module = Government.instance_eval { @superclass } | |
government.metaclass.instance_eval { @superclass = included_module } | |
included_module.instance_eval { @superclass = Government } | |
Government.instance_eval { @superclass = Object } | |
# Step 3. Testing | |
puts government.metaclass.superclass_chain.inspect | |
# => [#<IncludedModule People>, Government, Object, #<IncludedModule Kernel>] <-------- cool, prepended module is before Class | |
puts government.power | |
# => belongs to people <--------- this is right |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment