新手问题 ActiveSupport-4.1.0 的 autoreload

haroldrandom · July 13, 2017 · Last by haroldrandom replied at July 14, 2017 · 1024 hits

Hi,各位 最近在看《Metaprogramming Ruby 2》的 ActiveRecord 一节,发现一个疑问,在 google 了一番之后仍找不到答案,所以来这里试试看看~

ActiveRecord 4.1.0 版本中,的 active_record.rb 文件里,有这样几行代码

module ActiveRecord
  extend ActiveSupport::Autoload

  autoload :Base

  # ...
end

然后问再去看 ActiveSupport 4.1.0 的代码,发现 源码如下

module Autoload
  def self.extended(base) # :nodoc:
    base.class_eval do
      @_autoloads = {}
      @_under_path = nil
      @_at_path = nil
      @_eager_autoload = false
    end
 end

    def autoload(const_name, path = @_at_path)
      unless path
        full = [name, @_under_path, const_name.to_s].compact.join("::")
        path = Inflector.underscore(full)
    end

     if @_eager_autoload
        @_autoloads[const_name] = path
     end

      super const_name, path
    end

#...
end

我的疑惑是,autoload 函数里的 name 是从哪里来的?

项目跑起来,加个 byebug 跟进去看一下呗

Class 的 name 吧,跟一下比较好。

Reply to ericguo

Class 的 name 属性在哪里的呢?我没有找到,目前在尝试用 byebug 跟踪

发现了很早之前类似的问题,已经解答了,谢谢

You need to Sign in before reply, if you don't have an account, please Sign up first.