在非 Rails 项目中,我可以activesupport
独立安装。当我需要它很酷的猴子补丁功能时:
require 'active_support/core_ext'
但它是如何工作的?它是否自动需要active_support/core_ext中的所有文件?怎么Ruby
知道他们在这里?
在非 Rails 项目中,我可以activesupport
独立安装。当我需要它很酷的猴子补丁功能时:
require 'active_support/core_ext'
但它是如何工作的?它是否自动需要active_support/core_ext中的所有文件?怎么Ruby
知道他们在这里?
这就是(以我的平台为例):
> 宝石哪一个 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
。
但是没有require
不能加载目录。