include vs. require vs. require_dependency

I wrote about include/require in my blog a while back ...

I've never used require_dependency ...


Michal Gabrukiewicz wrote:

That's internal Rails stuff. More or less it means "load this library and monitor the constants it defines".

If your file follows Rails conventions it is picked up automatically and class reloading works out of the box.

If the file does not follow them, for example "application.rb" would be called "application_controller.rb" if it did, you can still load it and get the benefits of automatic class reloading via require_dependency.

I don't know whether is meant for public consumption though, since it is not documented. Just follow Rails file naming conventions and let Dependencies do its job.

-- fxn

Nice simple explanation. Thanks,

By the way, and totally off-topic, to anybody wondering about the name "ghoti," if I recall correctly, it's an alternate "spelling" of the word fish, using the gh from laugh, the o from women, an the ti from nation.
It was constructed by somebody (I don't remember who) a long long time ago complaining about spelling in English. I hadn't thought of it in many years until I saw it here. :slight_smile:

thanks for the detailed explanation ...
could you briefly outline the "naming conventions"

Sure, take the fully-qualified name of the class you want Rails to autoload and run this:

    $ script/console
    Loading development environment.
    >> "FooBar::Woo".underscore + '.rb'
    => "foo_bar/woo.rb"

That says that if the class FooBar::Woo is defined in a file named "foo_bar/woo.rb" living inmediately below some standard application directory like app/controllers Rails will find the file and load it for you automatically.

For example, if you define the User class in the file user.rb under app/models you don't need to require 'user'. Note you would in regular Ruby.

-- fxn

Not really. It applies to the contents listed in Dependencies.load_path, which in addition to app subdirs include lib and a few others. Check it out in the console.

That list may be further customised via config.load_paths in environment.rb.

-- fxn