What is the opposite of Ruby's include? What is the opposite of Ruby's include? ruby ruby

What is the opposite of Ruby's include?


No. You can't un-include a mixin in the Ruby Language. On some Ruby Implementations you can do it by writing an implementation specific extension in C or Java (or even Ruby in the case of Rubinius), though.


It's not really the same thing, but you can fake it with something like this:

module A  def hello    puts "hi!"  endendclass B   include Aendclass C  include AendB.new.hello # prints "Hi!"class Module  def uninclude(mod)    mod.instance_methods.each do |method|      undef_method(method)    end  endendclass B  uninclude AendB.new.hello rescue puts "Undefined!" # prints "Undefined!"C.new.hello  # prints "Hi!"

This may work in the common case, but it can bite you in more complicated cases, like where the module inserts itself into the inheritance chain, or you have other modules providing methods named the same thing that you still want to be able to call. You'd also need to manually reverse anything that Module.included?(klass) does.


Try http://github.com/yrashk/rbmodexcl, which provides an unextend method for you.