Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails “什么是……”&引用;在“前面”:Rails::Engine“;卑鄙_Ruby On Rails_Ruby - Fatal编程技术网

Ruby on rails “什么是……”&引用;在“前面”:Rails::Engine“;卑鄙

Ruby on rails “什么是……”&引用;在“前面”:Rails::Engine“;卑鄙,ruby-on-rails,ruby,Ruby On Rails,Ruby,我正在查看,其中一个代码块如下所示: module Blorgh class Engine < ::Rails::Engine isolate_namespace Blorgh end end 模块Blorgh 类引擎

我正在查看,其中一个代码块如下所示:

module Blorgh
  class Engine < ::Rails::Engine
    isolate_namespace Blorgh
  end
end
模块Blorgh
类引擎<::Rails::引擎
隔离名称空间Blorgh
结束
结束
::Rails::Engine
是什么意思?我知道这可能是一个微不足道的Ruby问题,但是,我似乎在任何地方都找不到任何东西


谢谢。

从层次上考虑所有模块和类:它在顶层寻找“Rails”,在下一层寻找“Engine”

e、 g:

::Rails
选择第一个代码段中的模块,它位于顶层


::Rails::Engine
选择顶级
Rails
模块中的类
Engine

前面的
::
表示在全局名称空间的顶部而不是
Blorgh
名称空间中查找
Rails::Engine

没有对比,没有本代码中的

module Blorgh
  class Engine < Rails::Engine
    isolate_namespace Blorgh
  end
end
模块Blorgh
类引擎

第二行将查找
Blorgh::Rails
,您将得到一个错误,
namererror:uninitialized constant Blorgh::Rails

,因此,Ray完全正确。我只想再举一个例子

假设我们有一个名为Foo(original,right?)的引擎,它安装在一个名为Bar的主机应用程序中,类似于:

#bar/config/routes.rb
Rails.application.routes.draw do
  ...
  mount Foo::Engine, at: '/'
  ...
end
Foo有一个应用程序控制器:

#foo/app/controllers/foo/application_controller.rb
module Foo
  class ApplicationController < ActionController::Base
    ...
    def foo_action
      ...
    end
    ...
  end
end
#bar/app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
  ...
  def bar_action
    ...
  end
  ...
end
#foo/app/controllers/foo/application#u controller.rb
模块Foo
类ApplicationController
Bar有一个应用程序控制器:

#foo/app/controllers/foo/application_controller.rb
module Foo
  class ApplicationController < ActionController::Base
    ...
    def foo_action
      ...
    end
    ...
  end
end
#bar/app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
  ...
  def bar_action
    ...
  end
  ...
end
#bar/app/controllers/application#u controller.rb
类ApplicationController
现在,让我们假设Foo引擎中有两个控制器,如:

#foo/app/controllers/foo/baz_controller.rb
module Foo
  def BazController < ApplicationController
    ...
  end
end
#foo/app/controllers/foo/baz#u controller.rb
模块Foo
def控制器<应用程序控制器
...
结束
结束

#foo/app/controllers/foo/bif_controller.rb
模块Foo
def控制器<::应用程序控制器
...
结束
结束
BazController
继承自
ApplicationController
(前面没有
)。这意味着它在当前名称空间(
Foo
)中查找
ApplicationController
。因此它将在
foo::ApplicationController
中定义
foo\u操作


BifController
继承自
::ApplicationController
前面)。这意味着它在全局名称空间中查找
ApplicationController
,在本例中,全局名称空间是主机应用程序
Bar
。因此,它将在bar的
ApplicationController
中定义
bar\u操作

symbolhound.com对这类问题很有帮助。@muistooshort我看不出这个问题与您标记为现有文章的问题是如何重复的。@ChrisPeters
在开始时与其他地方的意思相同,答案甚至涵盖了前面的
案例。如果你想要或者去寻找一个更好的副本,你可以不同意。@muistooshort我认为重新打开这个问题是有效的,因为他在一个符号前面明确地问这是什么意思。我也认为重新打开这个问题是有效的。链接答案“排序”回答了这个问题,但不足以直接指导操作。常量前面的
引用回主机应用程序。例如,在引擎中,
class FooController<::ApplicationController
表示从主机应用程序中的
ApplicationController
继承。但是,所提到的问题并没有使这一点非常清楚。一个更清晰的答案是很有价值的。我以前从来都不知道这一点!:好的解释,谢谢!