我正在尝试查找嵌套命名空间的根类/模块。
这是找到它的最有效的方法吗?我不喜欢我正在转换为字符串。似乎应该有一个更优雅的解决方案。
class Foo
class Bar
def parent
Object.const_get self.class.to_s.split(/::/).first
end
end
end
Foo::Bar.new.parent #=> Foo发布于 2011-03-25 03:52:02
有一个Module.nesting
module Foo
module Bar
module Baz
p Module.nesting # => [Foo::Bar::Baz, Foo::Bar, Foo]
p Module.nesting.last # => Foo
end
end
endhttps://stackoverflow.com/questions/5423709
复制相似问题