From: Robert Dober Date: 2010-04-30T20:27:27+09:00 Subject: Re: Switching dynamically between methods (inside modules) On Fri, Apr 30, 2010 at 12:50 PM, Paul A. wrote: A first, elegant approach suffers from the minor defect that it does not work ;( module A def a; "A" end end module B def a; "B" end end o = Object::new o.extend A puts o.a o.extend B puts o.a o.extend A puts o.a thus more work is needed the following code will give you some ideas, I hope class Module def apply obj this_mod = self methods = instance_methods( false ).map{ | mname | [ mname, instance_method( mname ) ] } p methods class << obj; self end.module_eval do include this_mod methods.each do | name, mthd | define_method name do | *args, &blk | mthd.bind( self ).call( *args, &blk ) end end end end end module A def a; "A" end end module B def a; "B" end end class Object def force_extend *mods mods.each do | mod | mod.apply self end end end o = Object::new o.force_extend A puts o.a o.force_extend B puts o.a o.force_extend A puts o.a HTH R. -- The best way to predict the future is to invent it. -- Alan Kay