Leon's Blogging

Coding blogging for hackers.

Ruby - Double Colon(::)

| Comments

在 ruby 中常常會看到各種符號, 像是 :: 在 ruby 就像是 namespace 的感覺。 :: 加在最前面,就是會去參照最上面的 namespace

有點像是一個是絕對路徑,一個是相對路徑

1
2
3
4
5
::A::B # like /A/B
# is an absolute path to the constant.

A::B # like ./A/B
# is a path relative to the current tree level.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
COUNT = 0        # constant defined on main Object class
module Foo
  COUNT = 0
  ::COUNT = 1    # set global COUNT to 1
  COUNT = 2      # set local COUNT to 2
end

# 因爲 constant 通常是不能變動的,但在 ruby 只會做緊告
# (irb):4: warning: already initialized constant COUNT
# (irb):1: warning: previous definition of COUNT was here
# (irb):5: warning: already initialized constant Foo::COUNT
# (irb):3: warning: previous definition of COUNT was here

puts COUNT       # this is the global constant
# => 1
puts Foo::COUNT  # this is the local "Foo" constant
# => 2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
module Rails
  class Engine
  end
end

module Foo
  module Rails
    class Engine
    end
  end

  class Engine1 < Rails::Engine
  end
  class Engine2 < ::Rails::Engine
  end
end

Foo::Engine1.superclass
# => Foo::Rails::Engine
Foo::Engine2.superclass
# => Rails::Engine

參考文件:

Comments