Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditionally setting require: true in Gemfile

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
like image 394
Nick Tomlin Avatar asked May 26 '26 10:05

Nick Tomlin


1 Answers

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!

like image 80
Veets Avatar answered May 30 '26 02:05

Veets