这个双冒号::是什么?例如Foo:酒吧。

我找到了一个定义:

::是一个一元操作符,允许在类或模块内定义的:常量、实例方法和类方法从类或模块外的任何地方访问。

如果你只能使用::来暴露任何东西,那么作用域(私有的,受保护的)有什么用呢?


当前回答

Ruby on rails使用::进行名称空间解析。

class User < ActiveRecord::Base

  VIDEOS_COUNT = 10
  Languages = { "English" => "en", "Spanish" => "es", "Mandarin Chinese" => "cn"}

end

使用它:

User::VIDEOS_COUNT
User::Languages
User::Languages.values_at("Spanish") => "en"

另外,其他用法是:当使用嵌套路由时

OmniauthCallbacksController在用户下定义。

并路由为:

devise_for :users, controllers: {omniauth_callbacks: "users/omniauth_callbacks"}


class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController

end

其他回答

Ruby on rails使用::进行名称空间解析。

class User < ActiveRecord::Base

  VIDEOS_COUNT = 10
  Languages = { "English" => "en", "Spanish" => "es", "Mandarin Chinese" => "cn"}

end

使用它:

User::VIDEOS_COUNT
User::Languages
User::Languages.values_at("Spanish") => "en"

另外,其他用法是:当使用嵌套路由时

OmniauthCallbacksController在用户下定义。

并路由为:

devise_for :users, controllers: {omniauth_callbacks: "users/omniauth_callbacks"}


class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController

end

下面这个简单的例子说明了这一点:

MR_COUNT = 0        # constant defined on main Object class
module Foo
  MR_COUNT = 0
  ::MR_COUNT = 1    # set global count to 1
  MR_COUNT = 2      # set local count to 2
end

puts MR_COUNT       # this is the global constant: 1
puts Foo::MR_COUNT  # this is the local constant: 2

摘自http://www.tutorialspoint.com/ruby/ruby_operators.htm

加上前面的答案,使用::访问实例方法是有效的Ruby。以下均有效:

MyClass::new::instance_method
MyClass::new.instance_method
MyClass.new::instance_method
MyClass.new.instance_method

根据最佳实践,我认为只推荐最后一种。

Surprisingly, all 10 answers here say the same thing. The '::' is a namespace resolution operator, and yes it is true. But there is one gotcha that you have to realize about the namespace resolution operator when it comes to the constant lookup algorithm. As Matz delineates in his book, 'The Ruby Programming Language', constant lookup has multiple steps. First, it searches a constant in the lexical scope where the constant is referenced. If it does not find the constant within the lexical scope, it then searches the inheritance hierarchy. Because of this constant lookup algorithm, below we get the expected results:

module A
  module B
      PI = 3.14
      module C
        class E
          PI = 3.15
        end
        class F < E
          def get_pi
            puts PI
          end
        end
      end
  end
end
f = A::B::C::F.new
f.get_pi
> 3.14

F继承自E,而B模块在F的词法范围内。因此,F实例将引用模块B中定义的常数PI。现在,如果模块B没有定义PI,那么F实例将引用超类E中定义的PI常量。

但是如果我们使用'::'而不是嵌套模块呢?我们会得到同样的结果吗?不!

通过在定义嵌套模块时使用名称空间解析操作符,嵌套模块和类不再在其外部模块的词法范围内。如下所示,在A::B中定义的PI不在A::B::C::D的词法范围内,因此当我们试图在get_pi实例方法中引用PI时,会得到未初始化的常量:

module A
end

module A::B
  PI = 3.14
end

module A::B::C
  class D
    def get_pi
      puts PI
    end
  end
end
d = A::B::C::D.new
d.get_pi
NameError: uninitialized constant A::B::C::D::PI
Did you mean?  A::B::PI

不,它不是访问每个方法,它是一个“解析”操作符,也就是说,你用它来解析一个常量/静态符号的范围(或者你可以说的位置)。

例如,在第一行中,Rails使用它来查找ActiveRecord内部的基类。模块,在你的第二个模块中,它用于定位路由类的类方法(静态),等等。

它不是用来暴露任何东西,而是用来“定位”你瞄准镜周围的东西。

http://en.wikipedia.org/wiki/Scope_resolution_operator