-4

我想要一些类似A::B::C.nesting #=> [A::B::C, A::B, A]但在这些模块声明之外的东西......我怎样才能得到这个?ActiveSupport 已启用。谢谢。

4

1 回答 1

2
class Module
  def nesting
    a = inspect.split("::")
    a.length.downto(1).to_a.map{|l| const_get(a[0, l].join("::"))}
  end
end

或者

class Module
  def nesting
    s = inspect
    s.count(":")./(2).downto(0).to_a.map{|l| const_get(s[/[^:]+(?:::[^:]+){#{l}}/])}
  end
end
于 2013-09-10T14:50:47.513 回答