이이 파일 시스템에서 파일과 디렉토리와 같은 중첩 된 루비루비의 이중 콜론 (: :) 연산자 사용의 차이
module Foo
class Engine < Rails::Engine
end
end
및
module Foo
class Engine < ::Rails::Engine
end
end
이이 파일 시스템에서 파일과 디렉토리와 같은 중첩 된 루비루비의 이중 콜론 (: :) 연산자 사용의 차이
module Foo
class Engine < Rails::Engine
end
end
및
module Foo
class Engine < ::Rails::Engine
end
end
상수의 차이입니다. 따라서 상수는 경로에 따라 고유하게 식별됩니다.
는 파일 시스템과 비유를 그리려면
여기::Rails::Engine #is an absolute path to the constant.
# like /Rails/Engine in FS.
Rails::Engine #is a path relative to the current tree level.
# like ./Rails/Engine in FS.
이 가능한 오류의 그림입니다 :
module Foo
# We may not know about this in real big apps
module Rails
class Engine
end
end
class Engine1 < Rails::Engine
end
class Engine2 < ::Rails::Engine
end
end
Foo::Engine1.superclass
=> Foo::Rails::Engine # not what we want
Foo::Engine2.superclass
=> Rails::Engine # correct
Rails::Engine #is a path relative to the current tree level.
# like ./Rails/Engine in FS.
이것은 사실이 아니다! 당신이 등 현재 범위 또는 외부 범위 또는 외부 외부 범위
내에 있을지 M : Y 루비가 가장 가까운 (고화질)에 대한 상관없이 조회 말할 때 그래서module M
Y = 1
class M
Y = 2
class M
Y = 3
end
class C
Y = 4
puts M::Y
end
end
end
# => 3
module M
Y = 1
class M
Y = 2
class C
Y = 4
puts M::Y
end
end
end
# => 2
module M
Y = 1
class M
Y = 2
class M
Y = 4
puts M::Y
end
end
end
# => 4
:
의 예로 들어 보자
어느 루비인가? 2.2.4에서 나는 3, 3, 4 그리고 많은 "최상위 레벨 상수가 이미 참조 된 경고"를 얻습니다. –
오른쪽. 그래서'Module Foo'라는 이름을 사용하는 모듈에서 Rails :: Engine을 호출한다면'Foo :: Rails :: Engine'으로 해석 할 수 있습니다. 나는 때때로 이런 에러를 보았고 include 앞에 추가로'::'을 추가해야했다. –
파일 시스템 유추는 훌륭합니다! 감사합니다 – CuriousMind
그것은 Paolo Perrotta의 "Metaprogramming Ruby"라는 책에서 가져 왔습니다. 루비에 대한 지식을 깊게하는 매우 유용한 책. – Flexoid