The role of bundler with gem dependencies
The very first gem I ever created was actually pretty good. Apart from one thing: when you installed it, it didn't install the dependencies (i.e. the other gems that it depended upon). This might seem like a pretty big oversight, and it was, but there's a good reason. I actually did specify the dependencies - I just did it in the wrong place.
Bundler gives you a way of generating a gem skeleton, which you can use to quickly get started with a gemspec, Rakefile, lib directory and more. It also adds a Gemfile to your gem's root directory:
I'd been doing rails development for quite a while by this point, so I knew what a Gemfile was for: specifying your dependencies. Surely, you just add your gems to the Gemfile?
Then you just run bundle install
and it installs the json gem.
What the Gemfile actually does
If you do this, you will find that your dependencies aren't installed when someone installs your gem with gem install
:
The expected result is that it also installs json. The reason that it doesn't is that you must actually specify your dependencies in the gemspec, not the Gemfile:
And you can leave it out of your Gemfile. In this case, gem install
gives:
What's the point of using bundler when creating a gem?
Bundler makes it easy to install gems when you're developing. It will read your gemspec and include any dependencies that you've added, so that you can run bundle install
. You don't even need to use the Gemfile for development dependencies, such as test frameworks or plugins: use add_development_dependency
in the gemspec for that.
I don't know of any good reason to add something to the Gemfile when creating a gem - in fact, it's more likely to cause you trouble in the long run.