Skip to content

Commit 930dddb

Browse files
committed
Edits to the autoloading guide
1 parent 89ceb31 commit 930dddb

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

guides/source/autoloading_and_reloading_constants.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Introduction
2020

2121
INFO. This guide documents autoloading, reloading, and eager loading in Rails applications.
2222

23-
In a normal Ruby program, dependencies need to be loaded by hand. For example, the following controller uses classes `ApplicationController` and `Post`, and normally you'd need to put `require` calls for them:
23+
In an ordinary Ruby program, you explictly load the files that define classes and modules you want to use. For example, the following controller refers to `ApplicationController` and `Post`, and you'd normally issue `require` calls for them:
2424

2525
```ruby
2626
# DO NOT DO THIS.
@@ -35,7 +35,7 @@ class PostsController < ApplicationController
3535
end
3636
```
3737

38-
This is not the case in Rails applications, where application classes and modules are just available everywhere:
38+
This is not the case in Rails applications, where application classes and modules are just available everywhere without `require` calls:
3939

4040
```ruby
4141
class PostsController < ApplicationController
@@ -45,9 +45,10 @@ class PostsController < ApplicationController
4545
end
4646
```
4747

48-
Idiomatic Rails applications only issue `require` calls to load stuff from their `lib` directory, the Ruby standard library, Ruby gems, etc. That is, anything that does not belong to their autoload paths, explained below.
48+
Rails _autoloads_ them on your behalf if needed. This is possible thanks to a couple of [Zeitwerk](https://github.com/fxn/zeitwerk) loaders Rails sets up on your behalf, which provide autoloading, reloading, and eager loading.
49+
50+
On the other hand, those loaders do not manage anything else. In particular, they do not manage the Ruby standard library, gem dependencies, Rails components themselves, or even (by default) the application `lib` directory. That code has to be loaded as usual.
4951

50-
To provide this feature, Rails manages a couple of [Zeitwerk](https://github.com/fxn/zeitwerk) loaders on your behalf.
5152

5253
Project Structure
5354
-----------------
@@ -231,7 +232,7 @@ While booting, applications can autoload from the autoload once paths, which are
231232

232233
However, you cannot autoload from the autoload paths, which are managed by the `main` autoloader. This applies to code in `config/initializers` as well as application or engines initializers.
233234

234-
Why? Initializers only run once, when the application boots. They do not run again on reloads. If the application used a reloadable class in an initializer, and the class was edited, on reload changes would not be reflected in whatever the initializer did. Therefore, referring to reloadable constants during initialization is disallowed.
235+
Why? Initializers only run once, when the application boots. They do not run again on reloads. If an initializer used a reloadable class or module, edits to them would not be reflected in that initial code, thus becoming stale. Therefore, referring to reloadable constants during initialization is disallowed.
235236

236237
Let's see what to do instead.
237238

0 commit comments

Comments
 (0)