Difference between 'require bundler' and &

2019-02-25 01:19发布

问题:

When I use

require 'bundler/setup' 

i get Bundler.with_clean_env is not supported.

But when I change this to

require 'bundler' 

It supports Bundler.with_clean_env. The confusion that rises here is what is the difference between requiring 'bundler' and 'bundler/setup'?

回答1:

When referring to gems, require 'foo' would require the foo.rb file that is located in the gem's lib directory. That file has usually the same name as the gem and is responsible for requiring all other necessary files for the gem to function.

When you do require 'foo/bar', you search for lib/foo/bar.rb. In other words, you require only a specific file from that gem and not the whole thing.


The bundler/setup is responsible for loading all gems described in your Gemfile. Bundler.with_clean_env is a completely different functionality, defined in the gem's main file.



回答2:

Gemfiles can include groups like :test or :development.

  • require 'bundler/setup' includes all groups from your Gemfile.

  • require 'bundler' on the other hand lets you specify (via Bundler.setup) which groups to include.

From the documentation:

Configure the load path so all dependencies in your Gemfile can be required

require 'rubygems'
require 'bundler/setup'
require 'nokogiri'

Only add gems from specified groups to the load path. If you want the gems in the default group, make sure to include it

require 'rubygems'
require 'bundler'
Bundler.setup(:default, :ci)
require 'nokogiri'


标签: ruby bundler