ruby – 反映嵌套命名空间
发布时间:2020-12-17 03:27:18  所属栏目:百科  来源:网络整理 
            导读:我试图找到嵌套命名空间的根类/模块. 这是找到它的最有效方法吗?我不喜欢我转换成字符串.似乎应该有一个更优雅的解决方案. class Foo class Bar def parent Object.const_get self.class.to_s.split(/::/).first end endendFoo::Bar.new.parent #= Foo 解决
                
                
                
            | 
 我试图找到嵌套命名空间的根类/模块. 
  
  这是找到它的最有效方法吗?我不喜欢我转换成字符串.似乎应该有一个更优雅的解决方案. class Foo
   class Bar
     def parent
        Object.const_get self.class.to_s.split(/::/).first
     end
   end
end
Foo::Bar.new.parent #=> Foo解决方法
 有Module.nesting 
  
  
  module Foo
  module Bar
    module Baz
      p Module.nesting       # => [Foo::Bar::Baz,Foo::Bar,Foo]
      p Module.nesting.last  # => Foo
    end
  end
end(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! | 
