Lai Yu-Hsuan
Lai Yu-Hsuan

Reputation: 28121

why can I require 'active_support/core_ext'?

In a non-rails project, I can just install activesupport standalone. When I need its cool monkeypatch features:

require 'active_support/core_ext'

But how does it work? Does it automatically require all files in active_support/core_ext? How Ruby know they are here?

Upvotes: 2

Views: 2099

Answers (2)

Casper
Casper

Reputation: 34328

This is how (example for my platform):

> gem which 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
> cat 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}" }

So you see that core_ext.rb is the file that will "autoload" every *.rb file in the core_ext directory.

Upvotes: 3

Azolo
Azolo

Reputation: 4383

Look for the file named core_ext.rb.

But no require can't load directories.

Upvotes: 2

Related Questions