I have found through trial and error that several steps are required, with the help of ActiveSupport
.
Add
activesupport
as a dependency in the.gemspec
filesspec.add_dependency 'activesupport'
Include ActiveSupport::Dependencies in the top-level module of your gem (this was the most elusive requirement)
require 'bundler'; Bundler.setup require 'active_support/dependencies' module MyGem unloadable include ActiveSupport::Dependencies end require 'my_gem/version.rb' # etc...
Set up your gem to use autoloading. You an either manually use ruby autoload declarations to map symbols into filenames, or use the Rails-style folder-structure-to-module-hierarchy rules (see ActiveSupport #constantize)
In each module and class in your gem, add
unloadable
.module MyModule unloadable end
In each file that depends on a module or class from the gem, including in the gem itself, declare them at the top of each file using
require_dependency
. Look up the path of the gem as necessary to properly resolve the paths.require_dependency "#{Gem.loaded_specs['my_gem'].full_gem_path}/lib/my_gem/myclass"
If you get exceptions after modifying a file and making a request, check that you haven't missed a dependency.
For some interesting details see this comprehensive post on Rails (and ruby) autoloading.