1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

autoloading guide: Kernel#autoload -> Module#autoload [ci skip]

This commit is contained in:
Xavier Noria 2014-12-15 10:21:27 +01:00
parent 5c37c9a5c0
commit 4ffa699799

View file

@ -15,7 +15,7 @@ After reading this guide, you will know:
* How constant reloading works
* That autoloading is not based on `Kernel#autoload`
* That autoloading is not based on `Module#autoload`
* Solutions to common autoloading gotchas
@ -645,24 +645,24 @@ what changed since dependencies between classes makes that really tricky.
Instead, everything is wiped.
Kernel#autoload isn't Involved
Module#autoload isn't Involved
------------------------------
`Kernel#autoload` provides a lazy way to load constants that is fully integrated
`Module#autoload` provides a lazy way to load constants that is fully integrated
with the Ruby constant lookup algorithms, dynamic constant API, etc. It is quite
transparent.
Rails internals make extensive use of it to defer as much work as possible from
the boot process. But constant autoloading in Rails is **not** implemented with
`Kernel#autoload`.
`Module#autoload`.
One possible implementation based on `Kernel#autoload` would be to walk the
One possible implementation based on `Module#autoload` would be to walk the
application tree and issue `autoload` calls that map existing file names to
their conventional contant name.
There are a number of reasons that prevent Rails from using that implementation.
For example, `Kernel#autoload` is only capable of loading files using `require`,
For example, `Module#autoload` is only capable of loading files using `require`,
so reloading would not be possible. Not only that, it uses an internal `require`
which is not `Kernel#require`.
@ -672,7 +672,7 @@ again. Also, it doesn't support qualified names, so files with namespaces should
be interpreted during the walk tree to install their own `autoload` calls, but
those files could have constant references not yet configured.
An implementation based on `Kernel#autoload` would be awesome but, as you see,
An implementation based on `Module#autoload` would be awesome but, as you see,
at least as of today it is not possible. Constant autoloading in Rails is
implemented with `Module#const_missing`, and that's why it has its own contract,
documented in this guide.