ruby-on-rails - Ruby 2.0 如何在包含模块后从模块中取消包含模块?

标签 ruby-on-rails ruby metaprogramming

module X
end

module Y
end

module Z
  #TODO include X replacement of including Y
  #TODO include Y replacement of including X
end

有没有办法解决 ruby​​ 不包含 uninclude 关键字的问题??

最佳答案

如果您真的需要这种功能,您可以使用 refinements 来实现.

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

关于ruby-on-rails - Ruby 2.0 如何在包含模块后从模块中取消包含模块?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19461978/

相关文章:

ruby-on-rails - 如何在 Controller 中呈现 View 并分配给变量 (Rails)

java - 自定义 Java 类加载器未用于加载依赖项?

python - 如何从当前 python 模块动态获取类集?

ruby-on-rails - Rails 验证是否存在两个字段之一 (XOR)

ruby-on-rails - 添加用户名以设计 rails 4

ruby-on-rails - RSpec 未定义方法 "errors_on"

python - “classmethod”对象在 Python 3 的元类方法中不可调用

ruby-on-rails - Activerecord has_many :through through multiple models

ruby-on-rails - ActionController::Parameters 'require' 方法返回一个字符串

ruby-on-rails - RoR : undefined method `http_basic_authenticate_with'