Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想要一些类似A::B::C.nesting #=> [A::B::C, A::B, A]但在这些模块声明之外的东西......我怎样才能得到这个?ActiveSupport 已启用。谢谢。
A::B::C.nesting #=> [A::B::C, A::B, A]
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