在Rails中,require,require_dependency和常量重载有何关系? [英] How are require, require_dependency and constants reloading related in Rails?

查看:148
本文介绍了在Rails中,require,require_dependency和常量重载有何关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

requirerequire_dependency有何不同?
require_dependency如何自动重新加载开发中的类,但require不能?

How are require and require_dependency different?
How can require_dependency automatically reload classes in development but require can't ?

我研究了Rails的ActiveSupport::Dependencies和dispatcher.rb代码.我在require_dependency的代码中看到的基本上是将常量添加到autoloaded_constants数组中.但是每次请求后,它都会在调度程序内的clear_application中清除.

I digged into Rails' ActiveSupport::Dependencies and dispatcher.rb code. What I saw in require_dependency's code is it basically adds the constants to an autoloaded_constants array. But it gets cleared in clear_application inside dispatcher after each request.

有人可以给我一个清晰的解释或向我指出一些会有所帮助的资源吗?

Can someone give a clear explanation or point me to some resources which will help?

推荐答案

require(及其表亲load)是Ruby的核心方法. require_dependency是帮助Rails处理依赖关系管理问题的方法.长话短说,它允许Rails在开发模式下重新加载类,这样您就不必在每次更改代码时都重新启动服务器. Rails框架将require_dependency您的代码,以便在进行更改时可以跟踪并重新加载它.标准的Ruby require不会这样做.作为应用程序(或插件/引擎)开发人员,您不必担心require_dependency,因为它完全是Rails的内部功能.

require (and its cousin load) are core Ruby methods. require_dependency is a method that helps Rails handle the problem of dependency management. Long story short, it allows Rails to reload classes in development mode so that you don't have to restart the server each time you make a code change. The Rails framework will require_dependency your code so that it can track and reload it when changes are made. The standard Ruby require doesn't do that. As an app (or plugin/engine) developer you should not have to worry about require_dependency as this is purely internal to Rails.

Rails类加载过程的神奇之处在于ActiveSupport :: Dependencies模块.此代码扩展了默认的Ruby行为,以允许Rails应用程序内的代码使用Rails的路径和文件命名约定自动加载模块(包括从Module继承的类).这样一来,程序员无需像普通的Ruby应用程序中那样通过require调用来填充代码.

The magic of the Rails class loading process is in the ActiveSupport::Dependencies module. This code extends the default Ruby behavior to allow code inside your Rails app to automatically load modules (including classes which inherit from Module) using Rails' path and file naming conventions. This eliminates the need for the programmer to litter their code with require calls like you would in a plain Ruby application.

换句话说,这使您可以在文件app/models/admin/user.rb中定义class Admin::User,并使Rails从应用程序的另一部分(如控制器)调用Admin::User.new时知道您在说什么.没有ActiveSupport :: Dependencies,您将必须手动require所需的一切.

To put it another way, this lets you define class Admin::User inside the file app/models/admin/user.rb and have Rails know what you are talking about when you call Admin::User.new from another part of the application like a controller. Without ActiveSupport::Dependencies involved you would have to manually require everything you needed.

如果您来自诸如C#,Java等静态类型的语言,那么这可能会让人感到惊讶:Rails代码直到需要时才加载.例如,直到您尝试调用User.whatever_method_here之后,才会定义User模型类,并且不会加载user.rb. Rails可以防止Ruby抱怨缺少的常量,加载User的代码,然后允许Ruby继续正常运行.

If you are coming from a statically typed language like C#, Java, etc then this might be a surprise: Rails code is not loaded until it is needed. For example, a User model class isn't defined and user.rb isn't loaded until AFTER you try to call User.whatever_method_here. Rails prevents Ruby from complaining about that missing constant, loads code for User, and then allows Ruby to continue as normal.

虽然我不能满足您的特定需求,但是如果您实际上需要在插件或引擎中使用require_dependency方法,我会感到非常惊讶.如果您遵循Rails约定,则也不必手动调整$ LOAD_PATH.这不是"Rails方式".

While I can't speak for your specific need, I would be very surprised if you actually needed to use the require_dependency method from within a plugin or engine. If you follow Rails conventions you shouldn't have to tweak the $LOAD_PATH by hand, either. This is not "the Rails way".

在Ruby以及Rails的世界中,简单和清晰是关键.如果您要做的只是编写一个插件或引擎,而您已经深入研究内部结构,那么您可以考虑从另一个角度解决问题.我的直觉告诉我,您可能正在尝试做一些不必要的复杂工作.但是话又说回来,我不知道你到底在做什么! :)

In the world of Ruby and also Rails simplicity and clarity is key. If all you want to do is write a plugin or engine and you are already diving deep into internals then you may consider approaching your problem from a different angle. My gut tells me that you may be trying to do something that is needlessly complicated. But then again, I have no clue what you are doing exactly!! :)

这篇关于在Rails中,require,require_dependency和常量重载有何关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆