如何查找包含模块的模块或类?

时间:2016-05-09 23:28:23

标签: ruby

有一个很好的技巧可以帮助找到继承的子类:

(defrule check-property
    ?room <- (object (is-a ROOM)
                     (property ?property))
    (test (integerp ?property))
    =>
    (printout ?*debug-print* "Room " ?room " has property"  crlf))

此外,我发现使用class SubClasses @@subclasses ||= [] def self.inherited subclass @@subclasses << subclass end def self.subclasses @@subclasses end end 查找包含在子类中的模块很有用。

但它在Foo.included_modules上无法使用。我如何获取包含该模块的所有类?有可能吗?

更新

解决!

Getting a list of classes that include a module

1 个答案:

答案 0 :(得分:2)

要监视模块何时包含在另一个模块或类中,请使用included钩子:

module Parent
  class << self
    attr_reader :includers
  end

  def self.included(base)
    @includers ||= []
    @includers << base.name
  end
end