Ruby 2.0如何在包含模块后从模块中取出模块?

zot*_*guy 11 ruby metaprogramming ruby-on-rails

module X
end

module Y
end

module Z
  #TODO include X replacement of including Y
  #TODO include Y replacement of including X
end
Run Code Online (Sandbox Code Playgroud)

有没有办法解决ruby不包含uninclude关键字的事实?

在此输入图像描述

dav*_*nes 7

如果你真的需要这种功能,你可以通过使用改进来实现.

class Foo
end

module X
  def x
    puts 'x'
  end
end

module Y
end

module R
  refine Foo do
    include X
    include Y
  end
end

# In a separate file or class
using R
# Foo now includes X and Y
Foo.new.x

# In a different file or class
# Foo no longer includes X and Y
Foo.new.x # NoMethodError
Run Code Online (Sandbox Code Playgroud)