2

I'd like to conditionally set require: in my Gemfile definition depending on the environment to work in conjunction with Bundler.require. E.g. i'd like pry to be available in all environments but only have require: true set in development and test. I'm currently doing something like this:

# make pry available for anyone who wants it
# but not automatically required
# e.g. in a console
gem 'pry', require: false

# automatically require pry
# for easy usage in tests
group :development, :test do
  gem 'pry', require: true
end

Which works but results in a nice warning regarding a duplicate gem definition:

Your Gemfile lists the gem pry (>= 0) more than once. You should probably keep only one of them. While it's not a problem now, it could cause errors if you change the version of just one of them later.

Alternately, I can try to do something like this:

gem 'pry', require: ENV["RACK_ENV"] != "production"

but that feels much less declarative (and a little gross).

Edit:

To clarify,

  1. I want to be to have pry available via Bundler.require in the development and test environments
  2. Be available in production (e.g. for use in a console) without being automatically required via Bundler.require
Nick Tomlin
  • 28,402
  • 11
  • 61
  • 90
  • `require: true` is not a valid option. See http://stackoverflow.com/questions/4800721/bundler-what-does-require-false-in-a-gemfile-mean for an explanation of what the `require` option does. – user513951 Dec 10 '15 at 01:28
  • I think it is valid according to https://github.com/bundler/bundler/pull/2538 (it's just an alias) although perhaps it has been removed in later versions of bundler? – Nick Tomlin Dec 10 '15 at 01:46
  • Ah. It makes sense to `require: someboolean` that can be `true` or `false`, but to use `require: true` with the literal `true` is redundant. `gem 'pry'` and `gem 'pry', require: true` are both equivalent to `gem 'pry', require: 'pry'`. – user513951 Dec 10 '15 at 01:57

3 Answers3

1

You use :require => false when you want the gem to be installed but not "required". If you are not planning to use 'pry' gem in production it doesn't make sense to even install it in production.

You can make a gem available to use in any environment by explicitly specifying it. E.g. Make pry available in development and test environments

group :development, :test do
  gem 'pry'
end

Hope, this helps!

Veets
  • 171
  • 6
  • 2
    I am actually planning to use pry in production for a poor man's console so I want to have it available for a console script but not automatically required when my Sinatra application initialized – Nick Tomlin Dec 10 '15 at 15:31
  • Interesting! In that case just add `gem 'pry', :require => false` to your Gemfile. Also you will need to add `require 'pry' if (Bundler.definition.groups & [:development, :test]).any?` to your application.rb or app.rb or whatever. Good Luck! – Veets Dec 10 '15 at 20:31
  • If I set `require => false` in my Gemfile pry won't be available in my tests without explicitly `require`ing it though right? – Nick Tomlin Dec 11 '15 at 18:32
  • Yes, you're correct! And that is why you will need to add `require 'pry' if (Bundler.definition.groups & [:development, :test]).any?` to your application.rb – Veets Dec 11 '15 at 18:59
1

Grouping your dependencies allows you to perform operations on the entire group.

group :development, :test do
  gem 'pry', require: true
end

The above code itself says pry gem will be required in development and test environment only. Not in production. So you don't need to specify that it should not required in production.

If you mention gem 'pry' in your gem file without specifying anything then it will be required in all environment default.

For more information kindly refer this link : http://bundler.io/groups.html


From : The How and Why of Bundler Groups

Specifying groups allows you to do two things. First, you can install the gems in your Gemfile, minus specific groups. For instance, Rails puts mysql and pg in a database group so that if you’re just working on ActionPack, you can bundle install --without db and run the ActionPack tests without having to worry about getting the gems installed.

Second, you can list specific groups to autorequire using Bundler.require. By default, Bundler.require requires all the gems in the default group (which is all the gems that have no explicit group). You can also say Bundler.require(:default, :another_group) to require specific groups.

Gagan Gami
  • 10,121
  • 1
  • 29
  • 55
0

If you include the group name in your Bundler.require then the gems are loaded as well (except those that explicitly indicate require: false).

For example having a Gemfile with the following content:

gem 'redis'

group :test do
  gem 'pry'
  gem 'simplecov', require: false
end

If you do Bundler.require it will only require redis and if you do Bundler.require(:default, :test) it will require both pry and redis; notice that still simplecov won't be required even if you explicitly indicate the :test group. So to use simplecov you will still need to explicitly call require 'simplecov'

Mario Carrion
  • 671
  • 5
  • 11