Question

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.

Was it helpful?

Solution

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top