I have a question about the role of bundler when creating a gem. I’m
familiar with bundler and the way that it handles dependencies in a
rails app, but I’ve run into a few snags with using it to manage
dependencies for gems that I’ve created.
In a gemspec you can use add_dependency to include other gems as a
dependency for your gem. Random example:
Gem::Specification.new do |gem|
#…
gem.add_dependency ‘json’
end
That means that installing my gem will also install json (if it doesn’t
exist).
I’m pretty sure bundle new isn’t a thing. If it is for you, then maybe
you have an older version.
Urgh, my bad. I meant bundle gem mygem!
The gemspec is what tells it to look in mygem.gemspec for the
dependencies
list.
So does that mean that you don’t add gems to the Gemfile unless you only
want them for development? And that any dependencies have to go in
the gemspec?
You can include all (runtime + development) dependencies in the
gemspec. Rubygems allows you to specify development dependencies with #add_development_dependency, to distinguish it from #add_runtime_dependency.
Yes, I know that you specify dependencies this way in the gemspec, but
my question is whether you can do the same using the Gemfile?
So does that mean that you don’t add gems to the Gemfile unless you only
want them for development? And that any dependencies have to go in
the gemspec?
You can include all (runtime + development) dependencies in the
gemspec. Rubygems allows you to specify development dependencies with #add_development_dependency, to distinguish it from #add_runtime_dependency.
The reason I brought this up was because I incorrectly specified the
dependencies in my first gem. After doing bundle gem ... and seeing
the Gemfile I incorrectly assumed that my dependencies would go in that
Gemfile, in the same way that they do for a rails app.
I only learnt that this isn’t the correct way to specify dependencies
after installing my gem on another machine.