ITeye博客

ruby module :: 前置操作符

selfcontroller · 2014-04-02 16:55 · ruby on rails · 编程语言 · Markdown

在ruby代码中,我们经常看到有类似这样的获取module的用法:


::Net::HTTP

那么,为什么不直接使用这种方式呢:


Net::HTTP

这里有一个很好的解答,就利用这个例子,我们来看一下:


module Music
  module Record
    # perhaps a copy of Abbey Road by The Beatles?
  end

  module EightTrack
    # like Gloria Gaynor, they will survive!
  end
end

module Record
  # for adding an item to the database
en

为了在Music这个module外访问 Record这个module,我们必须这样:


Music::Record

如果我们在EightTrack这个module中访问 Music 中的 Recordmodule,我们可以直接使用Record:


Record

ruby会到当前的scope中去寻找这个module

但是如果我们想在EightTrack这个module中访问Music这个module之外的这个Record的话,我们要使用


::Record

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。