简体   繁体   English

Ruby:自动加载方法有什么作用?

[英]Ruby: What does autoload method do?

module ActionController extend ActiveSupport::Autoload

  autoload :Base
  autoload :Caching
  autoload :Metal
  autoload :Middleware
end

Can anyone elaborate with example/sample output what autoload method does?谁能用示例/示例 output 详细说明自动加载方法的作用?

Autoload ensures that the class or module is automatically loaded if needed. Autoload可确保在需要时自动加载类或模块。 There is a nice article of Peter Cooper named "Ruby Techniques Revealed: Autoload" that explains the differences to require. Peter Cooper的一篇很好的文章名为“Ruby Techniques Revealed:Autoload” ,它解释了需要的差异。 I don't want to repeat his example here :-) 我不想在这里重复他的例子:-)

autoload is an alternative to require when the code to be executed is within a module. 当要执行的代码在模块中时,autoload是require的替代方法。 The main functional difference is when the code is actually executed. 主要功能区别在于代码实际执行时。 autoload is frequently used in ruby gems to speed up application load time. autoload经常用于ruby gems中,以加快应用程序的加载时间。

With autoload, the first time you use the module constant, it is loaded from the specified file. 使用自动加载,第一次使用模块常量时,它将从指定的文件加载。 With require, it is executed as soon as you require it. 有了require,它会在您需要时立即执行。 Note that the Ruby implementation of autoload requires both a module and filename, but the Rails version in your example makes the filename optional. 请注意,自动加载的Ruby实现需要模块和文件名,但示例中的Rails版本使文件名可选。

As far as an example goes, there really isn't much more than what you have in your question. 就一个例子而言,实际上没有比你的问题更多的东西了。 These modules would be executed when you use ActionController::Base, ActionController::Caching, etc. 当您使用ActionController :: Base,ActionController :: Caching等时,将执行这些模块。

Autoloading in Ruby在 Ruby 中自动加载

The Kernel#autoload method speeds up the initialization of your library by lazily loading the modules. Kernel#autoload方法通过延迟加载模块来加速库的初始化。 It won't load the library or framework code you don't need.它不会加载您不需要的库或框架代码。

The Ruby version takes two arguments: module (can be either a string or a symbol) and filename (string), and registers the filename to be loaded the first time that module is accessed. Ruby 版本采用两个 arguments:模块(可以是字符串或符号)和文件名(字符串),并在第一次访问该模块时注册要加载的文件名。

autoload :Payment, "lib/modules/payment.rb"

Autoloading in Rails在 Rails 中自动加载

Ruby on Rails provides its own autoload method via its Active Support framework by overriding Ruby's autoload method. Ruby on Rails 通过其 Active Support 框架通过覆盖 Ruby 的自动autoload方法来提供自己的自动autoload方法。

It allows you to define autoload based on Rails naming conventions.它允许您根据 Rails 命名约定定义自动加载。 It automatically guesses the filename based on the module name.它会根据模块名称自动猜测文件名。

autoload :SecurePassword

If the path is not provided, Rails guesses the path by joining the constant name with the current module name and generating its underscored and lowercase form.如果未提供路径,Rails 会通过将常量名称与当前模块名称连接起来并生成其下划线和小写形式来猜测路径。 Finally, it calls Ruby's autoload method by calling super and passes the module name and the generated path.最后,它通过调用 super 调用 Ruby 的 autoload 方法,并传递模块名称和生成的路径。

For more details, check out: Autoloading Modules in Rails有关更多详细信息,请查看:在 Rails 中自动加载模块

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM