本文介绍了获取包含模块的类的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个mixin,我想获取一个包含它的所有类的列表.在mixin模块中,我执行了以下操作:
I have a mixin for which I would like to get a list of all the classes that have included it. In the mixin module, I did the following:
module MyModule
def self.included(base)
@classes ||= []
@classes << base.name
end
def self.classes
@classes
end
end
class MyClass
include MyModule
end
这很好用:
> MyModule.classes #=> nil
> MyClass.new #=> #<MyClass ...>
> MyModule.classes #=> ["MyClass"]
现在,我想将此部分提取到一个单独的模块中,该模块可以包含在我的其他mixins中.因此,我提出了以下建议:
Now, I would like to extract this part out into a separate module that can be included in my other mixins. So, I came up with the following:
module ListIncludedClasses
def self.included(base)
p "...adding #{base.name} to #{self.name}.classes"
@classes ||= []
@classes << base.name
base.extend(ClassMethods)
end
def self.classes
@classes
end
module ClassMethods
def included(module_base)
p "...adding #{module_base.name} to #{self.name}.classes"
@module_classes ||= []
@module_classes << module_base.name
super(module_base)
end
def classes
@module_classes
end
end
end
module MyModule
include ListIncludedClasses
end
但这不起作用,因为从ListIncludedClasses添加到MyModule的#included(module_base)方法永远不会运行.有趣的是,它确实已将#classes成功添加到MyModule.
This doesn't work though, because the #included(module_base) method being added to MyModule from ListIncludedClasses is never getting run. Interestingly enough, it does successfully add #classes to MyModule.
> MyModule.classes #=>
"...adding Rateable to ListIncludedClasses.classes"
=> nil
> ListIncludedClasses #=> ["MyModule"]
> MyClass.new #=> #<MyClass ...>
# ^^ THIS SHOULD BE ADDING MyClass TO MyModule.classes ^^
> MyModule.classes #=> nil
我想念什么?
推荐答案
module MyMod; end
class A; include MyMod; end
class B < A; end
class C; end
ObjectSpace.each_object(Class).select { |c| c.included_modules.include? MyMod }
#=> [B, A]
这篇关于获取包含模块的类的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!