public
Created

  • Download Gist
gistfile1.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
# 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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.