简体   繁体   English

Ruby:模块A与类A :: B

[英]Ruby: module A vs class A::B

So, I can have something like this ( A ): 因此,我可以得到类似以下内容( A ):

module A
    class B
    end
end

and also I can have this ( B ): 我也可以有这个( B ):

class A::B
end

Why would I use A instead of B and vice versa? 为什么我用A代替B ,反之亦然?

Because in option B module A must be already defined. 因为必须在选项B中定义模块A。 There are also some other issues with this option. 此选项还存在其他一些问题。 You can read more about it here http://techblog.thescore.com/how-you-nest-modules-matters-in-ruby/ 您可以在http://techblog.thescore.com/how-you-nest-modules-matters-in-ruby/了解更多信息

When you are defining A::B for the first time, you have to take the first option. 首次定义 A::B时,必须采用第一个选项。 Otherwise, The second option saves indentation and is more compact, which is useful when you want to write something to A::B concisely and independent of other things in A . 否则, 第二个选项将保存缩进并且更加紧凑,当您希望简洁地且不依赖于A的其他内容向A::B写入内容时 此选项很有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM