You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: guides/source/autoloading_and_reloading_constants.md
+6-5Lines changed: 6 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -20,7 +20,7 @@ Introduction
20
20
21
21
INFO. This guide documents autoloading, reloading, and eager loading in Rails applications.
22
22
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:
24
24
25
25
```ruby
26
26
# DO NOT DO THIS.
@@ -35,7 +35,7 @@ class PostsController < ApplicationController
35
35
end
36
36
```
37
37
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:
39
39
40
40
```ruby
41
41
classPostsController < ApplicationController
@@ -45,9 +45,10 @@ class PostsController < ApplicationController
45
45
end
46
46
```
47
47
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.
49
51
50
-
To provide this feature, Rails manages a couple of [Zeitwerk](https://github.com/fxn/zeitwerk) loaders on your behalf.
51
52
52
53
Project Structure
53
54
-----------------
@@ -231,7 +232,7 @@ While booting, applications can autoload from the autoload once paths, which are
231
232
232
233
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.
233
234
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.
0 commit comments