3 ms·
Would someone more familiar with Ruby mind explaining what Zeitwerk is/does? I think I have a vague idea, but both the announcement post linked to from this one
by mintplant 7y ago
Would someone more familiar with Ruby mind explaining what Zeitwerk is/does? I think I have a vague idea, but both the announcement post linked to from this one and the project's README assume a lot of prior knowledge about the use case.
- cookrn 7y agoSure, I can try! One part of the Rails "magic" that the framework tries to abstract away from an app developer is code loading. So, instead of each file in your app needing to explicitly specify using "require" statements which other files it needs to run, Rails uses Ruby to automatically "require" files for you. This system is dependent on constant names and namespaces matching file names and directory structures. So, a constant (Rails model) like "Sports::Player" used in a controller might be found via lookup automatically in "app/models/sports/player.rb". A system which does this has been a part of Rails for a _very_ long time, and Zietwerk is an upgrade to it by a longtime Rails contributor that fixes some of the previous downsides and gotchas.
- bogdanu 7y agoOh, so it's similar to PHP's autoloading based on psr4.
- Dangeranger 7y agoSure, I'll give it a shot. Ruby classes usually need to explicitly require the other classes and modules which they use. Rails implemented a different way to achieve requiring dependencies using a method called "const_missing" which requires the missing dependent when it is not found in the known constant list. The problem with this is that if your classes and modules do not adhere to a strict arrangement within your source code directories then Rails can sometimes not find the file you are looking for, or worse find the wrong file. Since "require" has an effect on the loaded constant namespace, the order of requiring dependencies can make a difference in the presence of bugs related to which code has been required. There's more information here: https://github.com/fxn/zeitwerk#motivation https://github.com/fxn/zeitwerk#motivation