Permalink
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
104 lines (89 sloc) 3.73 KB
---
title: How to use Bundler with Ruby
---
.container.guide
%h2 How to use Bundler with Ruby
.contents
.bullet
.description
Configure the load path so all dependencies in
your Gemfile can be required
:code
# lang: ruby
require 'rubygems'
require 'bundler/setup'
require 'nokogiri'
.bullet
.description
Only add gems from specified groups to the
load path. If you want the gems in the
default group, make sure to include it
:code
# lang: ruby
require 'rubygems'
require 'bundler'
Bundler.setup(:default, :ci)
require 'nokogiri'
= link_to 'Learn More: Groups', './groups.html', class: 'btn btn-primary'
%h2 Compatibility
.contents
.bullet
.description
Ruby 2.0 and RubyGems 2.0 both require Bundler 1.3 or later. If you have questions about compatibility between Bundler and your system, please check the compatibility list.
= link_to 'Learn More: Compatibility', '/compatibility.html', class: 'btn btn-primary'
%h2#setting-up-your-application-to-use-bundler
Setting Up Your Application to Use Bundler
.bullet
.description
Bundler makes sure that Ruby can find all of the gems in the <code>Gemfile</code>
(and all of their dependencies). If your app is a Rails app, your default application
already has the code necessary to invoke bundler.
.bullet
.description
For another kind of application (such as a Sinatra application), you will need to set up
bundler before trying to require any gems. At the top of the first file that your
application loads (for Sinatra, the file that calls <code>require 'sinatra'</code>), put
the following code:
:code
# lang: ruby
require 'rubygems'
require 'bundler/setup'
.bullet
.description
This will automatically discover your <code>Gemfile</code>, and make all of the gems in
your <code>Gemfile</code> available to Ruby (in technical terms, it puts the gems "on the
load path"). You can think of it as an adding some extra powers to <code>require
'rubygems'</code>.
.bullet
.description
Now that your code is available to Ruby, you can require the gems that you need. For
instance, you can <code>require 'sinatra'</code>. If you have a lot of dependencies, you
might want to say "require all of the gems in my <code>Gemfile</code>". To do this, put
the following code immediately following <code>require 'bundler/setup'</code>:
:code
# lang: ruby
Bundler.require(:default)
For our example Gemfile, this line is exactly equivalent to:
:code
# lang: ruby
require 'rails'
require 'rack-cache'
require 'nokogiri'
.bullet
.description
Astute readers will notice that the correct way to require the <code>rack-cache</code>
gem is <code>require 'rack/cache'</code>, not <code>require 'rack-cache'</code>. To tell
bundler to use <code>require 'rack/cache'</code>, update your Gemfile:
:code
# lang: ruby
source 'https://rubygems.org'
gem 'rails', '5.0.0'
gem 'rack-cache', require: 'rack/cache'
gem 'nokogiri', '~> 1.4.2'
.bullet
.description
For such a small <code>Gemfile</code>, we'd advise you to skip
<code>Bundler.require</code> and just require the gems by hand (especially given the
need to put in a <code>:require</code> directive in the <code>Gemfile</code>). For much
larger <code>Gemfile</code>s, using <code>Bundler.require</code> allows you to skip
repeating a large stack of requirements.