bundler
17 Apr 2017groups
gems outside any named group are in default
group.
install gems from all groups:
$ bundle
install gems from all groups except production
:
$ bundle --without production
if your app is gem dependent gems are added to gemspec file instead of
Gemfile using add_runtime_dependency
(kind of default
group) or
add_development_dependency
(kind of development
group) methods.
these dependencies are then included in Gemfile using gemspec
directive.
in this case Gemfile might contain only test
or custom groups.
require
require all gems from groups default
and production
:
Bundler.require(:default, :production)
Rails app by default requires default
and group corresponding to
current environment in config/application.rb:
# Rails.groups = [:default, Rails.env]
Bundler.require(*Rails.groups)
in plain Ruby app you have 2 options:
-
require all gems from all or selected groups in lib/my_app.rb (common entry point of CLI application or gem)
# require gems from all groups Bundler.require # require gems from default and production groups Bundler.require(:default, :production)
current environment can be fetched from custom environment variable - you’ve got to set it manually when launching app.
this method is recommended for large
Gemfile
s. -
require gems manually in appropriate files where they are used
- gems from
test
group (say,rspec
) are required in spec/spec_helper.rb - gems which are used in Rakefile only (say,
rubocop
) are required only there pry-byebug
gem can be required in lib/my_app.rb only ifDEBUG
environment variable is set
this method is recommended for smaller
Gemfile
s. - gems from
setup
http://bundler.io/bundler_setup.html
bundler setup configures the load path so that gems from all or specified groups can be required.
-
configure the load path for all gems:
require 'bundler/setup'
-
configure the load paths for gems from specified groups:
Bundler.setup(:default, :development)
-
in theory you should first configure the load path and then require gems:
require 'bundler/setup' Bundler.require
but in practice I don’t configure the load path as described above - all gems can be required without configuring the load path first.
UPDATE: I encountered situations when gem cannot be loaded because it’s not found without calling
require 'bundler/setup'
first - this is the case forruby-fifo
gem and my own gemfiles
. thus I had to configure the load path before requiring gems by addingrequire 'bundler/setup'
to the beginning of lib/my_app.rb file.