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.

···

--
Posted via http://www.ruby-forum.com/.

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:

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

···

On Fri, Apr 5, 2013 at 6:39 AM, Jon Cairns <lists@ruby-forum.com> wrote:

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

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

···

--
Posted via http://www.ruby-forum.com/\.

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

https://github.com/JoshCheek/mountain_berry_fields\.\.\.
https://github.com/JoshCheek/mountain_berry_fields\.\.\.

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?

···

--
Posted via http://www.ruby-forum.com/\.

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

···

--
Posted via http://www.ruby-forum.com/\.

Josh Cheek 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

···

--
Posted via http://www.ruby-forum.com/\.

You *can* but why would you?

-Josh

···

On Fri, Apr 5, 2013 at 7:07 AM, Jon Cairns <lists@ruby-forum.com> wrote:

> 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

There isn't one

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

-Josh

···

On Fri, Apr 5, 2013 at 7:15 AM, Jon Cairns <lists@ruby-forum.com> wrote:

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

> https://github.com/JoshCheek/mountain_berry_fields\.\.\.
> https://github.com/JoshCheek/mountain_berry_fields\.\.\.

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 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 http://guides.rubygems.org/specification-reference/#add_development_dependency

···

On 5 April 2013 12:54, Jon Cairns <lists@ruby-forum.com> 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?

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 Fri, Apr 5, 2013 at 6:54 AM, Jon Cairns <lists@ruby-forum.com> wrote:

Josh Cheek 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

--
Posted via http://www.ruby-forum.com/\.

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

···

On 5 April 2013 13:31, Josh Cheek <josh.cheek@gmail.com> wrote:

There isn't one
https://github.com/rubygems/rubygems/blob/e15f37c3a5918e2403a191a1c3ae9966fc835cf9/lib/rubygems/specification.rb#L1247

I'd go with add_runtime_dependency, though, since it's more explicit and in
the docs:
http://guides.rubygems.org/specification-reference/#add_runtime_dependency