Creating Lightweight Ruby Gems with Zeitwerk as a Development-Only Dependency
The article provides a tutorial on how to create a Ruby gem with Zeitwerk as a development-only dependency. It addresses the problem of managing code loading in non-Rails Ruby projects, where having many specialized classes in separate files can lead to a lot of require statements. Zeitwerk is introduced as an efficient and thread-safe code loader for Ruby, commonly used in Rails and Hanami. The tutorial explains how to use Zeitwerk in any Ruby project, including creating gems, by adding it to the Gemspec and making a few code modifications. It also addresses the concern of dependency minimalization and offers a solution to use Zeitwerk for development without including it as a runtime gem dependency. The author shares their experience of implementing this approach in their own gem, cryptreboot, which allows a machine with an encrypted disk to reboot by requesting a passphrase beforehand. Overall, the tutorial provides valuable insights for developers looking to optimize code loading in their Ruby projects.