Skip to content

Instantly share code, notes, and snippets.

@DanielVartanov
Created March 26, 2011 12:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DanielVartanov/888224 to your computer and use it in GitHub Desktop.
Save DanielVartanov/888224 to your computer and use it in GitHub Desktop.
# 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