これの違いは何ですか:
module Outer
module Inner
class Foo
end
end
end
この:
module Outer::Inner
class Foo
end
end
後者の例がOuter
以前に定義されていないと機能しないことはわかっていますが、一定のスコープには他にもいくつかの違いがあり、SOまたはドキュメント(プログラミングRubyブックを含む)でそれらの説明を見つけることができました
keymone の回答のおかげで、私は正しい Google クエリを作成し、これを見つけました: Module.nesting and constant name resolution in Ruby
::
変更定数スコープ解決の使用
module A
module B
module C1
# This shows modules where ruby will look for constants, in this order
Module.nesting # => [A::B::C1, A::B, A]
end
end
end
module A
module B::C2
# Skipping A::B because of ::
Module.nesting # => [A::B::C2, A]
end
end
少なくとも1つの違いがあります-定数ルックアップ、次のコードを確認してください:
module A
CONST = 1
module B
CONST = 2
module C
def self.const
CONST
end
end
end
end
module X
module Y
CONST = 2
end
end
module X
CONST = 1
module Y::Z
def self.const
CONST
end
end
end
puts A::B::C.const # => 2, CONST value is resolved to A::B::CONST
puts X::Y::Z.const # => 1, CONST value is resolved to X::CONST