Xavier Noria - Zeitwerk Internals - Rails World 2023

Learn the internals of Zeitwerk, a Ruby library that enhances the require mechanism with automatic reloading, custom loaders, and more, and discover how it's used by popular Ruby libraries and frameworks including Rails.

Key takeaways
  • In object-oriented programming, a top-level module is an object that is available at the top level of a program.
  • Ruby’s require mechanism is designed to autoload constants on demand.
  • Zeitwerk is a library that enhances Ruby’s require mechanism to provide more features, such as automatic reloading of code.
  • Zeitwerk achieves its goals by introducing the concept of a “loader” which is responsible for loading code on demand.
  • Loaders are recursive and can load code from multiple directories.
  • Zeitwerk introduces the concept of “autoloads” which are used to specify which code should be loaded on demand.
  • Autoloads are triggered when a constant is referenced that is not already loaded.
  • Zeitwerk provides a generic API that allows developers to create custom loaders.
  • The API is used to create a loader that is responsible for loading code from specific directories.
  • Zeitwerk provides a registry that keeps track of which loader is responsible for loading code from which directories.
  • Zeitwerk provides a way to unload code from memory to free up memory when it is no longer needed.
  • Zeitwerk provides a way to reload code automatically when it changes.
  • Zeitwerk is designed to be transparent and does not modify the code it loads.
  • Zeitwerk is used by many popular Ruby libraries and frameworks, including Rails.
  • Zeitwerk is a powerful tool that can provide significant benefits for developers who use it.
  • Zeitwerk is well-documented and has a strong community of developers who contribute to it.