如何跟踪完整的序列和序列? Ruby 应用程序中“require”的顺序是树吗?

发布于 2024-07-24 19:41:02 字数 197 浏览 2 评论 0原文

如何显示 Ruby 应用程序中发生的“require”的层次结构?

某些文件需要需要其他文件的文件。

但是,通过在调试模式下运行应用程序,您仅触发所需文件的子集 - 仅触发您的应用程序在任何给定时间点使用的任何功能子集所使用的文件。

如何以树的形式显示应用程序中所有需求的综合层次结构?

How can you display the hierarchy of 'require's that take place in a Ruby app?

Some files require files which require additional files.

However, by running an application in debug mode you only trigger a subset of required files - only the ones that are used by whatever subset of functionality your application is using at any given point in time.

How could you display a comprehensive hierarchy of all of the requires in an application as a tree?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏至、离别 2024-07-31 19:41:02

问题在于,在开发模式下,所有文件都使用 load 加载,而不是 require ,以便可以在每次请求时重新加载它们。 在生产中它们仅加载一次。 除了一些框架类之外,大多数文件仍然只在第一次使用时加载。 发生这种情况是因为 ActiveSupport 覆盖 const_missing 以自动尝试从具有适当命名方案的文件加载未知常量(ConstantName.to_s.underscore 将给出 require 'constant_name')。 这当然确实混淆了“require”层次结构。

对于一个简单的情况,您可以修改以下内容来满足您的一些需求(还可以检查 active_support 中的依赖项)

  $require_level = []
  alias :orig_require :require
  def require(file)
    puts "#{$require_level.join}#{file}"
    $require_level << "-"
    r = orig_require(file)
    $require_level.pop
    r
  end

  require 'foo'
  require 'baz'


 ben@legba-2:~ $ ruby check_requires.rb 
 foo
 -bar
 baz

祝您好运

编辑:解释

它的作用是创建一个全局数组来存储需求的嵌套级别。 第一个 put 输出所需的文件。 然后将破折号添加到嵌套级别。 然后实际上需要该文件。 如果加载的文件调用 require,则整个过程将再次开始,只不过嵌套级别为 1 深,因此“-#{file}”被 put-ed。 这个过程会重复进行,除非随着嵌套级别的增加,破折号也会增加。 加载文件及其所有依赖项后,require 会去掉它添加的破折号,以便嵌套级别处于与 require 启动时相同的状态。 这可以保持树结构的准确性。

const_missing 与 method_missing 类似。 基本上,就像当你调用 AnObject.some_unknown_method 时,ruby 会在引发 NoMethodError 之前调用 AnObject.method_missing(:some_unknown_method) 一样,使用 SomeUnknownConstant 会触发 const_missing(:SomeUnknownConstant) ) 在引发 NameError 之前。 Rails 定义了 const_missing,以便它将在某些指定路径中搜索可能定义缺失常量的文件。 它使用命名约定来促进这一点,例如 SomeUnknownConstant 预计位于 some_unknown_constant.rb 中。

有一种方法可以解决这种疯狂的情况。

The issue is that in development mode, all files are loaded with load rather than require so that they can be reloaded on each request. In production they are loaded only once. With the exception of some of the framework classes, most files are still only loaded when they are first used. This happens because ActiveSupport overrides const_missing to automatically attempt to load unknown constants from files with the appropriate naming scheme (ConstantName.to_s.underscore would give require 'constant_name'). This of course really muddles up the 'require' hierarchy.

For a trivial case, you can modify the following to meet some of your needs (also check out dependencies in active_support)

  $require_level = []
  alias :orig_require :require
  def require(file)
    puts "#{$require_level.join}#{file}"
    $require_level << "-"
    r = orig_require(file)
    $require_level.pop
    r
  end

  require 'foo'
  require 'baz'


 ben@legba-2:~ $ ruby check_requires.rb 
 foo
 -bar
 baz

Good luck

EDIT: Explanation

What that does is create a global array to store the nesting level of requires. The first puts outputs the required file. Then a dash is added to the nesting level. The file is then actually required. If the loaded file calls require, then this whole process starts again, except that nesting level is 1 deep so "-#{file}" is puts-ed. This process repeats except as the nesting level grows, so do the dashes. After a file and all of its dependencies are loaded, require takes off the dash that it added so that nesting level is in the same state it was when the require started. This keeps the tree structure accurate.

const_missing is similar to method_missing. Basically, just like when you call AnObject.some_unknown_method ruby will call AnObject.method_missing(:some_unknown_method) before raising a NoMethodError, using SomeUnknownConstant triggers a const_missing(:SomeUnknownConstant) before raising a NameError. Rails defines const_missing such that it will search certain specified paths for files that might define the missing constant. It uses a naming convention to facilitate this, e.g. SomeUnknownConstant is expected to be in some_unknown_constant.rb

There is a method to much of this rails madness.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文