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
Post a Comment