According to Katz,
When your gem is built, Rubygems will run that code and create a static representation. This means it’s fine to pull your gem’s version or other shared details out of your library itself. Do not, however, use other libraries or dependencies.
That means that require 'constants.rb'
and $LOAD_PATH.unshift(...)
etc are run when you build the gem. At runtime, it doesn't change the $LOAD_PATH
or cause a global require. Use gem.require_paths
instead to modify $LOAD_PATH
at runtime.
For example, in your gemspec, use
gem.files += Dir['config/**/*']
gem.require_paths = %w[lib config]
Then in places where $FOO
is required, use
require 'constants'
Side Notes
- You don't need to include the
.rb
extension when usingrequire
. - Make sure all the files you need are in
gem.files
.