From: Sean O'Halpin Date: 2011-12-04T09:19:36+09:00 Subject: Re: Inverse Operation of Module#include On Fri, Dec 2, 2011 at 6:57 AM, Su Zhang wrote: > Hi list, > > Is there a built-in inverse operation of Module#include? Currently I have: > [snip code] > > So if module B has already been mixed in, it cannot be re-included again > thus the foo method is not redefined to B#foo in the last include operation. > Thus occurs to me when I use Inversion of Control (or dependency injection) > to switch between various implementations of a certain interface. I was > thinking that if I can somehow "exclude" the module and include it again > then the problem would be solved, and hence the question. > > Or better yet, is there another way to achieve this kind of implementation > switching? > > Regards, > > Su > You could do this: #!/usr/bin/env ruby class A end module B def self.included(other) other.class_eval { def foo :b end } end end module C def self.included(other) other.class_eval { def foo :c end } end end a = A.new class A include B end p a.foo # => :b class A include C end p a.foo # => :c class A include B end p a.foo # => :b Regards, Sean