namespaces - Ruby: what does :: prefix do? -


i reading through source of artifice , saw:

module artifice   net_http = ::net::http   # ... end 

line: https://github.com/wycats/artifice/blob/master/lib/artifice.rb#l6

why not net::http instead of ::net::http, i.e., mean when use :: prefix?

the :: scope resolution operator. determines scope module can found under. example:

module music   module record     # perhaps copy of abbey road beatles?   end    module eighttrack     # gloria gaynor, survive!   end end  module record   # adding item database end 

to access music::record outside of music use music::record.

to reference music::record music::eighttrack use record because it's defined in same scope (that of music).

however, access record module responsible interfacing database music::eighttrack can't use record because ruby thinks want music::record. that's when use scope resolution operator prefix, specifying global/main scope: ::record.


Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -