2

Is this:

require 'bundler'
Bundler.setup

accomplishing the same as:

require 'bundler/setup'

As far as I understand, bundler/setup requires all groups automatically, while this isn't the case with require 'bundler'. So considering this fact, does that mean the above 2 snippets of code accomplish the same thing?

Casper
  • 33,403
  • 4
  • 84
  • 79
anemaria20
  • 1,646
  • 2
  • 17
  • 36

1 Answers1

0

The answer is found in the source code of bundler/setup:

require 'bundler/shared_helpers'

if Bundler::SharedHelpers.in_bundle?
  require 'bundler'
  if STDOUT.tty?
    begin
      Bundler.setup
    rescue Bundler::BundlerError => e
      ...
    end
  else
    Bundler.setup
  end

  ...    
end

The method in_bundle? seems to be checking if Bundler is being run inside itself (for testing purposes, as far as I could make out), and to verify that Gemfile exists.

So yes, for general use, both of your pieces of code are equivalent.

The advantage of the bundler/setup version, is that you can run Ruby from the command line like this:

ruby -rbundler/setup ... some_ruby_script.rb

Which will automatically make your script run under Bundler, even if the script itself might not be Bundler-aware, which is pretty much the same as what bundle exec does too.

Casper
  • 33,403
  • 4
  • 84
  • 79