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.