Ruby 2.0如何在包含模块后将其从模块中取消包含? [英] Ruby 2.0 How do I uninclude a module out from a module after including it?

查看:53
本文介绍了Ruby 2.0如何在包含模块后将其从模块中取消包含?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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关键字的事实?

Is there a way to work around the fact that ruby contains no uninclude keyword??

推荐答案

如果您确实需要这种功能,则可以使用

If you really need this kind of functionality, you could probably do it by using 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 2.0如何在包含模块后将其从模块中取消包含?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆