::ModuleName::ClassName 和 ModuleName::ClassName 有什么区别
在 ruby 中,我开始看到一种非常正常的做法,包括引用为 ::ModuleName::ClassName 的模块和 mixin,而在过去它几乎只是 ModuleName::ClassName。
我想要在这里得到的是对为什么这种做法最近越来越多地被看到以及它有什么不同的理解。
有什么区别?
有什么好处(如果先验者没有回答这个问题)?
预先感谢您的意见。
In ruby, I'm starting to see a pretty normal practice including modules and mixins referenced as ::ModuleName::ClassName, where in the past it was pretty much just ModuleName::ClassName.
What I'd like to get here is a decent understanding of why this practice is being seen more lately and what it does differently.
What's the difference?
What's the benefit (if the prior doesn't answer this)?
Thanks in advance for your input.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果将
::
放在开头,则指的是全局名称空间,如果不这样做,则指的是当前的名称空间。通常,如果您的类/模块中没有同名的类/模块,则不需要在开始时使用
::
。将打印出
If you put the
::
in the beginning you are referring to the global namespace, if you don't you are referring to your current namespace.Usually if you don't have a class/module with the same name inside your class/module you would not need to use the
::
in the beginning.will print out
当您执行
::ModuleName::ClassName
时,您会说:当你这样说
ModuleName::ClassName
时,你是在说:当我定义这样的代码时,我几乎总是使用 ::ModuleName::ClassName 来避免任何命名冲突。
When you do
::ModuleName::ClassName
you're saying:When you say it like this
ModuleName::ClassName
, you're saying:When I'm defining code like this I almost always use ::ModuleName::ClassName to avoid any naming conflicts.