0

可能重复:
Ruby 的双冒号 (::) 是什么意思?

原谅我的懒惰。我试着猜测。我不确定在这种情况下双 '::Logger' 做了什么?

https://github.com/wycats/rack-offline/blob/master/lib/rack/offline.rb#L25

似乎它正在初始化对象并将其分配给不在其范围内的变量?第 25 行被 {begin/end} 块包裹并分配给@logger

4

1 回答 1

3

就像/路径中的 a 定义嵌套目录一样,::访问嵌套类。

并且也类似于领导/,领导::意味着从树的最顶端开始。它开始在全局范围内搜索常量。

# Bar declared in global scope
class Bar
end

# Foo declared in global scope    
class Foo

  # A different class named Bar declared in the scope of Foo, not global
  class Bar
  end

  Bar   #=> refers to Foo::Bar, that is class Bar declared within Foo
  ::Bar #=> refers to outer global scope class named Bar

end
于 2013-01-12T05:30:58.430 回答