::ModuleName::ClassName 和 ModuleName::ClassName 有什么区别

发布于 2025-01-07 16:42:18 字数 218 浏览 1 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烛影斜 2025-01-14 16:42:18

如果将 :: 放在开头,则指的是全局名称空间,如果不这样做,则指的是当前的名称空间。

通常,如果您的类/模块中没有同名的类/模块,则不需要在开始时使用 ::

class Customer

  def to_s
    "Customer global"
  end

end


class Order

  class Customer
    def to_s
      "Customer within order"
    end
  end


  def initialize
    puts Customer.new
    puts ::Customer.new
  end


end

Order.new

将打印出

Customer within order
Customer global

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.

class Customer

  def to_s
    "Customer global"
  end

end


class Order

  class Customer
    def to_s
      "Customer within order"
    end
  end


  def initialize
    puts Customer.new
    puts ::Customer.new
  end


end

Order.new

will print out

Customer within order
Customer global
知足的幸福 2025-01-14 16:42:18

当您执行 ::ModuleName::ClassName 时,您会说:

我希望您在根命名空间中查找 ::ModuleName::ClassName ,忽略是否在另一个模块中找到此代码。因此,它总是会查找名为 ::ModuleName::ClassName 的类,而不会查找其他名称

当你这样说 ModuleName::ClassName 时,你是在说:

我希望您查找ModuleName::ClassName,但首先查看当前范围,然后再查看其他范围。因此,如果您有一个名为 MyModule 的模块,并且我的模块引用 ModuleName::ClassName ,那么首先尝试查找 MyModule::ModuleName::ClassName 然后尝试解析 ::模块名::类名.

当我定义这样的代码时,我几乎总是使用 ::ModuleName::ClassName 来避免任何命名冲突。

When you do ::ModuleName::ClassName you're saying:

I want you to look for ::ModuleName::ClassName at the root namespace, ignoring if this code is found inside another module. So, it will always look for a class that's named as ::ModuleName::ClassName and nothing else

When you say it like this ModuleName::ClassName, you're saying:

I want you to look for ModuleName::ClassName but looking at the current scope first and then at the other scopes. So, if you have a module called MyModule and this my module references ModuleName::ClassName then first try to find MyModule::ModuleName::ClassName then try to resolve ::ModuleName::ClassName.

When I'm defining code like this I almost always use ::ModuleName::ClassName to avoid any naming conflicts.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文