Role of bundler in creating and installing a gem

Hi fellow rubyists,

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).

$ gem install mygem
Successfully installed json-x.x.x
Successfully installed mygem-x.x.x

However, creating a gem with bundle new seems to want you to specify
dependencies in the Gemfile. So, if I do that:

Gemfile

gem ‘json’

And leave out the add_dependency in the gemspec, it doesn’t install
the dependencies when I run gem install:

$ gem install mygem
Successfully installed mygem-x.x.x

So is it possible to specify gem dependencies with bundler? Or is it
just for development purposes?

Thanks in advance.

On Fri, Apr 5, 2013 at 6:39 AM, Jon C. [email protected] wrote:

Gem::Specification.new do |gem|

However, creating a gem with bundle new seems to want you to specify
dependencies in the Gemfile. So, if I do that:

I’m pretty sure bundle new isn’t a thing. If it is for you, then maybe
you have an older version.

On mine, I would do bundle gem mygem and when I cat mygem/Gemfile it
says

source ‘https://rubygems.org

Specify your gem’s dependencies in mygem.gemspec

gemspec

The gemspec is what tells it to look in mygem.gemspec for the
dependencies
list.

-Josh

Josh C. wrote in post #1104528:

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?

Thanks

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?

I’m guessing it’s a no.

Thanks everyone

The only things I put in the Gemfile would be if they were on Github and
not rubygems.

Here is an example of a Gemfile and gemspec where you can see how I do
it.

-Josh

On 5 April 2013 12:54, Jon C. [email protected] wrote:

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.

See

On Fri, Apr 5, 2013 at 7:07 AM, Jon C. [email protected] wrote:

Thanks everyone

You can but why would you?

-Josh

On Fri, Apr 5, 2013 at 7:15 AM, Jon C. [email protected] wrote:

installing a gem?

There isn’t one

I’d go with add_runtime_dependency, though, since it’s more explicit and
in
the docs:

-Josh

Here is an example of a Gemfile and gemspec where you can see how I do
it.

GitHub - JoshCheek/mountain_berry_fields: Obviously it tests READMEs and other files with embedded code examples.
GitHub - JoshCheek/mountain_berry_fields: Obviously it tests READMEs and other files with embedded code examples.

That makes sense, thanks.

Incidentally, what’s the difference between add_dependency and
add_runtime_dependency? Is there a difference at the point of
installing a gem?

You can but why would you?

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.

Cheers guys

On 5 April 2013 13:31, Josh C. [email protected] wrote:

There isn’t one

I’d go with add_runtime_dependency, though, since it’s more explicit and in
the docs:
Specification Reference - RubyGems Guides

I agree with you, but it seems there’s a documentation mismatch on
docs.rubygems.org, not sure why but it’s probably confusing to people:
http://docs.rubygems.org/read/chapter/20