【问题标题】:what does ::Class in ruby do? [duplicate]ruby 中的 ::Class 有什么作用? [复制]
【发布时间】:2012-12-26 18:20:19
【问题描述】:

可能重复:
What is Ruby’s double-colon (::) all about?

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

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

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

【问题讨论】:

标签: ruby


【解决方案1】:

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

与前导/ 类似,前导:: 表示从树的最顶端开始。它开始在全局范围内搜索常量。

# 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

【讨论】:

    猜你喜欢
    • 2020-03-07
    • 1970-01-01
    • 2014-03-23
    • 2011-12-04
    • 1970-01-01
    • 2016-05-02
    • 2015-02-23
    • 2012-12-20
    • 1970-01-01
    相关资源
    最近更新 更多