문제

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