Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/64.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 RubyonRails:学习ActionController类-关于$:.unshift activesupport_路径和自动加载方法的问题_Ruby On Rails_Ruby - Fatal编程技术网

Ruby on rails RubyonRails:学习ActionController类-关于$:.unshift activesupport_路径和自动加载方法的问题

Ruby on rails RubyonRails:学习ActionController类-关于$:.unshift activesupport_路径和自动加载方法的问题,ruby-on-rails,ruby,Ruby On Rails,Ruby,在ActionController类(rails/actionpack/lib/action\u controller.lib)中,我发现了几个奇怪的代码。我真的没有一位导师可以向我学习Ruby on Rails,所以这个论坛是我唯一的希望: 问题1:有人能帮我解释一下这些代码行吗 begin require 'active_support' rescue LoadError activesupport_path = "#{File.dirname(__FILE__)}/../../ac

在ActionController类(rails/actionpack/lib/action\u controller.lib)中,我发现了几个奇怪的代码。我真的没有一位导师可以向我学习Ruby on Rails,所以这个论坛是我唯一的希望:

问题1:有人能帮我解释一下这些代码行吗

begin
  require 'active_support'
rescue LoadError
  activesupport_path = "#{File.dirname(__FILE__)}/../../activesupport/lib"
  if File.directory?(activesupport_path)
    $:.unshift activesupport_path
    require 'active_support'
  end
end
尤其是带有$:.unshift activesupport\u路径的行

在我看来,它试图要求使用active_支持类,如果这不起作用,它会检查active_路径是否是目录,如果是,那么。我完全失去了它

问题2:什么是自动加载方法

module ActionController
  # TODO: Review explicit to see if they will automatically be handled by
  # the initilizer if they are really needed.
  def self.load_all!
    [Base, CGIHandler, CgiRequest, Request, Response, Http::Headers, UrlRewriter, UrlWriter]
  end

  autoload :Base, 'action_controller/base'
  autoload :Benchmarking, 'action_controller/benchmarking'
  autoload :Caching, 'action_controller/caching'
  autoload :Cookies, 'action_controller/cookies'
  .
  .
  .
问题#3:如果我后来发现了一种我不了解的方法,那么如何找到最好的方法?至于那个自动加载方法案例,我试图在我的项目中找到它(我的Rails代码被冻结在那里),但找不到任何线索。我搜索了“def自动加载”。我做错事情了吗?是我的IDE吗,TextMate就是不剪


谢谢大家!

为了需要一个文件,您必须确保它的路径在Ruby
$LOAD\u path
变量中。这是一个从Perl继承而来的简写版本
$:

当您调用
require
时,解释器会在给定的每个路径中查找
.rb
文件,直到找到匹配项为止。如果它找到一个,它将被加载。如果不是,你会得到一个例外

您通常会在文件中看到这样的行:

# script/something

# This appends "script/../lib" to the $LOAD_PATH, but this expands to
# something like "/home/user/project/lib" depending on the details of
# your installation.
$: << File.expand_path(File.join('..', 'lib'), File.dirname(__FILE__))
使用
autoload
时,需要记住,类名总是在声明它的模块或类的范围内定义的。在本例中,
Bar
位于使用Ruby命名约定的
Foo
Foo::Bar

使用
Bar
类时,需要
foo/Bar.rb
文件。可以将其视为创建一个存根
Bar
类,一旦实际执行该类,该类将转换为真正的类

这是一种保持许多选项打开的好方法,有许多不同的模块可供使用,但不必预先将所有内容加载到内存中

至于第三个问题,像这样的可搜索文档将帮助您尝试查找有关方法的更多信息。Ruby和Rails之间的区别常常模糊不清,因此您可能必须检查这两个版本才能确定。Rails向核心Ruby类添加了很多方法,所以不要把可用的方法列表放在任何一方。它们协同工作


有时,当试图找出
methodname
的来源时,搜索
def methodname
是值得的,尽管这只包括常规声明。该方法可能是类似
method\u alias
的机制中的别名,也可能是使用
define\u method
动态创建的,在深入研究之前,您永远无法真正确定。不过,Rails中至少90%的方法是以传统方式声明的,因此大多数情况下,一个简单的搜索将产生您想要的结果。

我没有时间回答所有这些问题,但是#2:自动加载是ruby的惰性加载方式~哇,非常棒的完整答案,我真的欠您一个,非常感谢!我相信这将帮助许多RoR初学者,他们的目标是成为更高级的开发人员。
class Foo
  # Declare the class Foo::Bar to be defined in foo/bar.rb
  autoload(:Bar, 'foo/bar')
end