Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
# Step 1. Setting up environment
class Government
def power
"belongs to government"
module People
def power
"belogs to people"
Government.__send__ :include, self
government =
# 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