I want something like A::B::C.nesting #=> [A::B::C, A::B, A] but outside of these modules' declarations... How can I get this? ActiveSupport is enabled. Thanks.

有帮助吗?

解决方案

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

or

class Module
  def nesting
    s = inspect
    s.count(":")./(2).downto(0).to_a.map{|l| const_get(s[/[^:]+(?:::[^:]+){#{l}}/])}
  end
end
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top