2

在非 Rails 项目中,我可以activesupport独立安装。当我需要它很酷的猴子补丁功能时:

require 'active_support/core_ext'

但它是如何工作的?它是否自动需要active_support/core_ext中的所有文件?怎么Ruby知道他们在这里?

4

2 回答 2

3

这就是(以我的平台为例):

> 宝石哪一个 active_support
/usr/lib/ruby/gems/1.8/gems/activesupport-2.3.12/lib/active_support.rb
> cd /usr/lib/ruby/gems/1.8/gems/activesupport-2.3.12/lib/active_support
> 猫 core_ext.rb
filenames = Dir["#{File.dirname(__FILE__)}/core_ext/*.rb"].sort.map do |path|
  File.basename(path, '.rb')
end

filenames.each { |filename| require "active_support/core_ext/#{filename}" }

因此,您会看到core_ext.rb该文件将“自动加载”目录*.rb中的每个文件core_ext

于 2012-05-30T20:49:37.560 回答
2

查找名为core_ext.rb.

但是没有require不能加载目录。

于 2012-05-30T20:48:06.800 回答