In Progress
Unit 1, Lesson 1
In Progress

Bundler Tips and Tricks with Andre Arko

Bundler has become a staple part of the Ruby ecosystem over the years, to the point that we hardly think twice about it when working on a Ruby project. But over the years it has steadily gained new powers for easing Ruby development pains. Today guest chef André Arko joins us with some tips and tricks for accelerating your development process with recent and/or obscure Bundler features.

Video transcript & code

Some tools are so ubiquitous that we don't even think about them. For Ruby developers, Bundler is one of those tools. We use it to manage gem dependencies, and only give it a secon

 

d thought when it doesn't work the way we expected.

But Bundler is a tool that's still under active development, and it is gaining new and useful powers all the time. Here to tell us about some of the Bundler features you may have missed is guest chef André Arko. André is the leader of the Bundler team, he co-authored the third edition of The Ruby Way, and he founded Ruby Together, the Ruby trade association. At his day job, he provides expert development, architecture, and teaching through Cloud City Development. On weekends, he hunts for brunch spots in San Francisco that don’t have a wait.

Here he is to give you the inside scoop on advanced Bundler usage. Enjoy!


As a Ruby developer, chances are really good that you already know and use Bundler on a daily basis, and you can git pull && bundle install with the best of them. What you might not know is that Bundler has changed and grown over the last 8 years. The newer, lesser-known features can provide a lot of help with other gem-related workflows and problems. What other problems, you ask? Let’s take a look.


~/src/rubygems/rubygems.org $ bundle install
Using rake 12.0.0
Using concurrent-ruby 1.0.5
Using i18n 0.8.4
Using minitest 5.10.2
Using threadsafe 0.3.6
Using builder 3.2.3
Using erubis 2.7.0
Using miniportile2 2.1.0
Using rack 2.0.3
Using nio4r 2.0.0
Using websocket-extensions 0.1.2
Using mime-types 2.99.3
Using arel 7.1.4
Using publicsuffix 2.0.5
Using net-ssh 4.1.0
Using ast 2.3.0
Using execjs 2.7.0
Using aws-sigv4 1.0.0
Using jmespath 1.3.1
Using bcrypt 3.1.11
Using metaclass 0.0.4
Using hitimes 1.2.4
Using choice 0.2.0
Using bundler 1.14.6
Using methodsource 0.8.2
Using thor 0.19.4
Using compactindex 0.11.0
Using daemons 1.2.4
Using dalli 2.7.6
Using unfext 0.0.7.4
Using dynamicform 1.1.4
Using multijson 1.12.1
Using multipart-post 2.0.0
Using elasticsearch-dsl 0.1.5
Using hashie 3.5.5
Using elasticsearch-rails 0.1.9
Using ffi 1.9.18
Using gchartrb 0.8
Using gravtastic 3.2.6
Using highvoltage 3.0.0
Using highline 1.7.8
Using honeybadger 3.1.2
Using httpacceptlanguage 2.1.0
Using kgio 2.11.0
Using logstash-event 1.2.02
Using msgpack 1.1.0
Using netrc 0.11.0
Using newrelicrpm 4.2.0.334
Using parallel 1.11.2
Using pg 0.20.0
Using powerpack 0.1.1
Using ruby-graphviz 1.2.3
Using raindrops 0.18.0
Using trollop 2.1.2
Using rdoc 5.1.0
Using ruby-progressbar 1.8.1
Using unicode-displaywidth 1.2.1
Using sass 3.4.24
Using shoulda-context 1.2.1
Using statsd-instrument 2.1.2
Using toxiproxy 0.1.4
Using willpaginate 3.1.5
Using xml-simple 1.1.5
Using rainbow 2.2.2
Using tzinfo 1.2.3
Using nokogiri 1.7.2
Using rack-test 0.6.3
Using sprockets 3.7.1
Using rack-attack 5.0.1
Using rack-utf8sanitizer 1.3.2
Using websocket-driver 0.6.5
Using mail 2.6.6.rc1
Using addressable 2.5.1
Using net-scp 1.2.1
Using parser 2.4.0.0
Using autoprefixer-rails 7.1.1
Using uglifier 3.2.0
Using aws-sdk-core 2.9.28
Using mocha 1.2.1
Using timers 4.1.2
Using unf 0.1.4
Using elasticsearch-api 5.0.4
Using faraday 0.12.0.1
Using rbtrace 0.4.8
Using unicorn 5.3.0
Using activesupport 5.0.3
Using loofah 2.0.3
Using xpath 2.0.0
Using launchy 2.4.3
Using sshkit 1.13.1
Using rubocop 0.49.1
Using aws-sdk-resources 2.9.28
Using bourne 1.6.0
Using celluloid-essentials 0.20.5
Using celluloid-extras 0.20.5
Using celluloid-fsm 0.20.5
Using celluloid-pool 0.20.5
Using celluloid-supervision 0.20.6
Using domainname 0.5.20170404
Using elasticsearch-transport 5.0.4
Using rails-dom-testing 2.0.3
Using globalid 0.4.0
Using activemodel 5.0.3
Using delayedjob 4.1.3
Using factorygirl 4.8.0
Using shoulda-matchers 2.8.0
Using rails-html-sanitizer 1.0.3
Using capybara 2.14.0
Using airbrussh 1.2.0
Using aws-sdk 2.9.28
Using celluloid 0.17.3
Using http-cookie 1.0.3
Using elasticsearch 5.0.4
Using activejob 5.0.3
Using activerecord 5.0.3
Using emailvalidator 1.6.0
Using validatesformattingof 0.9.0
Using shoulda 3.5.0
Using actionview 5.0.3
Using capistrano 3.8.0
Using shoryuken 2.1.3
Using rest-client 2.0.2
Using elasticsearch-model 0.1.9
Using delayedjobactiverecord 4.1.1
Using rails-erd 1.5.2
Using actionpack 5.0.3
Using capistrano-bundler 1.2.0
Using actioncable 5.0.3
Using actionmailer 5.0.3
Using railties 5.0.3
Using sprockets-rails 3.2.0
Using capistrano-rails 1.2.3
Using factorygirlrails 4.8.0
Using jquery-rails 4.3.1
Using lograge 0.4.1
Using rails-i18n 5.0.4
Using rails 5.0.3
Using clearance 1.16.0
Using paulrevere 2.1.0
Using clearance-deprecatedpassword_strategies 1.10.1
Updating files in vendor/cache
Bundle complete! 58 Gemfile dependencies, 140 gems now installed.
Bundled gems are installed into ./.bundle.

Creating and releasing your own gems

The first thing Bundler can help with is making your own gems. It’s as easy as bundle gem foobar, and you end up with a new gem named foobar ready for you to add code.

There’s a one-time setup to tell Bundler if you want a rspec or minitest, an MIT license, or a code of conduct. After that, you can create gem after gem in just a few seconds each.

$ bundle gem foobar
Creating gem 'foobar'...
Do you want to generate tests with your gem?
Type 'rspec' or 'minitest' to generate those test files now and in the future. rspec/minitest/(none): rspec
Do you want to license your code permissively under the MIT license?
This means that any other developer or company will be legally allowed to use your code for free as long as they admit you created it. You can read more about the MIT license at http://choosealicense.com/licenses/mit. y/(n): y
MIT License enabled in config
Do you want to include a code of conduct in gems you generate?
Codes of conduct can increase contributions to your project by contributors who prefer collaborative, safe spaces. You can read more about the code of conduct at contributor-covenant.org. Having a code of conduct means agreeing to the responsibility of enforcing it, so be sure that you are prepared to do that. Be sure that your email address is specified as a contact in the generated code of conduct so that people know who to contact in case of a violation. For suggestions about how to enforce codes of conduct, see http://bit.ly/coc-enforcement. y/(n): y
Code of conduct enabled in config
      create  foobar/Gemfile
      create  foobar/lib/foobar.rb
      create  foobar/lib/foobar/version.rb
      create  foobar/foobar.gemspec
      create  foobar/Rakefile
      create  foobar/README.md
      create  foobar/bin/console
      create  foobar/bin/setup
      create  foobar/.gitignore
      create  foobar/.travis.yml
      create  foobar/.rspec
      create  foobar/spec/spec_helper.rb
      create  foobar/spec/foobar_spec.rb
      create  foobar/LICENSE.txt
      create  foobar/CODE_OF_CONDUCT.md
Initializing git repo in /Users/andre/Downloads/foobar

Any gem created by Bundler comes with a couple of nice touches: first, a bin/setup file that acts as a centralized, well-known location to install dependencies and do any other specific setup needed to develop on your library. By default, it creates a bash script that echoes commands, and runs bundle install, but it’s very easy to add your own commands.

$ cd foobar
$ cat bin/setup
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
set -vx

bundle install

# Do any other automated setup that you need to do here

Every gem also includes a bin/console, to load your gems and then launch IRB, Pry, Fir, or whatever interactive prompt you prefer. It’s the fastest way to experiment with the code from your gem.

$ cat bin/console
#!/usr/bin/env ruby

require "bundler/setup"
require "foobar"

# You can add fixtures and/or initialization code here to make experimenting
# with your gem easier. You can also use a different console, if you like.

# (If you use this, don't forget to add pry to your Gemfile!)
# require "pry"
# Pry.start

require "irb"
IRB.start(__FILE__)

Finally, every gem includes two extremely helpful rake tasks. The rake install will build your gem into a literal .gem file, and then use RubyGems to install it onto your local machine.

$ rake install
foobar 0.1.0 built to pkg/foobar-0.1.0.gem.
foobar (0.1.0) installed.
$ gem list foobar

*** LOCAL GEMS ***

foobar (0.1.0)

You can easily test that building, installing, and using your gem all work the way that you expect them to.

$ ruby -rfoobar -e 'puts Foobar::VERSION'
0.1.0

The other extremely useful task is rake release, which creates and pushes a git tag for your version, builds a .gem file, and releases the gem on RubyGems.org! What used to be an error-prone process that could take minutes is now just a single command and a few seconds. It’s marvelous.

$ rake release
foobar 0.1.0 built to pkg/foobar-0.1.0.gem.
Tagged v0.1.0.
Pushed git commits and tags.
Pushed foobar 0.1.0 to rubygems.org

Developing multiple repos at once

Now that you have a gem, what if you need to make changes to the gem and your app that depends on it at the same time? Bundler already has a feature to make this work as smoothly as possible: Local Git Repos.

To start, you tell Bundler where your local checkout of a git repo is. For example, we could continue to work on our foobar gem locally while using it in an application by running this configuration command in the application. Now that we’ve done that, running the application locally will use the code from our checkout. We can make changes, reload the application, and see them live.

$ cd app
$ bundle config local.foobar ~/src/indirect/foobar
$ bundle exec ruby -rfoobar -e 'puts Foobar::VERSION'
0.1.0

Adding gems

Now that we're up to speed on creating and using our own gems, the next tip is about speeding up using gems that already exist. Starting with Bundler 1.15, there is an add command that will automatically add a gem to your Gemfile and install it.

Given a gem name, Bundler will look up the gem by name, add it to your Gemfile, and then resolve and install your entire bundle.

Adding new gems to your application got easier starting with Bundler 1.15--now, you can simply run bundle add GEM and watch as Bundler adds the gem.

$ bundle add rack
Fetching gem metadata from https://rubygems.org/..............
Resolving dependencies...
Fetching gem metadata from https://rubygems.org/..............
Resolving dependencies...
Using bundler 1.16.1
Fetching rack 2.0.4
Installing rack 2.0.4

Now that the command has run, we can take a look inside the Gemfile using cat to see the changes that Bundler made.

$ cat Gemfile
source "https://rubygems.org"

gem "rack", "~> 2.0"

Adding gems is pretty basic so far, but we’re continuing to improve gem management from the command-line. Watch for this to keep getting better.

Editing installed gems

After we've installed all of our gems, it's a common wish to want to see the code for a gem directly. Bundler makes it easy to open any installed gem directly in your editor so you can see (or even edit) that gem's code.

When you run bundle open GEM, Bundler will look up the location of that gem on your machine, and then open it in your editor.

$ bundle open rack

The default editor is Vim, but Bundler will respect the EDITOR variable to open any editor you want.

Once you've got the gem open in your editor, you can browse the source for the gem, search for the definition of a method, and even edit that gem to change behavior or add debugging code. In this example, we're editing the the rack gem's main file, rack.rb. To show how this works, we'll change the VERSION constant.

Any changes that you make will be picked up by the next Ruby process you run. We can see the effect of our changes by printing the VERSION constant that we just edited.

$ bundle exec ruby -rrack -e 'p Rack::VERSION'
[5,0]

As you can probably imagine, being able to change your gems locally is an incredibly valuable tool for the times when it seems like the bug might be in a gem rather than in your own code.

Searching gems

If you’re not yet sure which gem to open, you can do a search across exactly the gems in this particular application by using the slightly-obscure command bundle show --paths. Combine that command with grep, ack, ripgrep, or your favorite search tool to get extremely precise results.

In this example, we're using the Rails app that powers RubyGems.org. Running bundle show --paths will print out the list of directories, one for each gem used by the application.

$ bundle show --paths
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/actioncable-5.0.3
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/actionmailer-5.0.3
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/actionpack-5.0.3
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/actionview-5.0.3
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/activejob-5.0.3
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/activemodel-5.0.3
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/activerecord-5.0.3
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/activesupport-5.0.3
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/addressable-2.5.1
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/airbrussh-1.2.0
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/arel-7.1.4
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/ast-2.3.0
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/autoprefixer-rails-7.1.1
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/aws-sdk-2.9.28
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/aws-sdk-core-2.9.28
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/aws-sdk-resources-2.9.28
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/aws-sigv4-1.0.0
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/bcrypt-3.1.11
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/bourne-1.6.0
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/builder-3.2.3
/Users/andre/.gem/ruby/2.3.3/gems/bundler-1.14.6/lib/gems/bundler-1.14.6
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/capistrano-3.8.0
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/capistrano-bundler-1.2.0
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/capistrano-rails-1.2.3
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/capybara-2.14.0
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/celluloid-0.17.3
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/celluloid-essentials-0.20.5
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/celluloid-extras-0.20.5
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/celluloid-fsm-0.20.5
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/celluloid-pool-0.20.5
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/celluloid-supervision-0.20.6
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/choice-0.2.0
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/clearance-1.16.0
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/clearance-deprecated_password_strategies-1.10.1
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/compact_index-0.11.0
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/concurrent-ruby-1.0.5
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/daemons-1.2.4
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/dalli-2.7.6
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/delayed_job-4.1.3
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/delayed_job_active_record-4.1.1
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/domain_name-0.5.20170404
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/dynamic_form-1.1.4
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/elasticsearch-5.0.4
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/elasticsearch-api-5.0.4
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/elasticsearch-dsl-0.1.5
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/elasticsearch-model-0.1.9
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/elasticsearch-rails-0.1.9
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/elasticsearch-transport-5.0.4
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/email_validator-1.6.0
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/erubis-2.7.0
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/execjs-2.7.0
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/factory_girl-4.8.0
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/factory_girl_rails-4.8.0
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/faraday-0.12.0.1
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/ffi-1.9.18
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/gchartrb-0.8
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/globalid-0.4.0
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/gravtastic-3.2.6
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/hashie-3.5.5
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/high_voltage-3.0.0
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/highline-1.7.8
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/hitimes-1.2.4
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/honeybadger-3.1.2
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/http-cookie-1.0.3
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/http_accept_language-2.1.0
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/i18n-0.8.4
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/jmespath-1.3.1
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/jquery-rails-4.3.1
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/kgio-2.11.0
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/launchy-2.4.3
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/lograge-0.4.1
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/logstash-event-1.2.02
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/loofah-2.0.3
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/mail-2.6.6.rc1
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/metaclass-0.0.4
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/method_source-0.8.2
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/mime-types-2.99.3
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/mini_portile2-2.1.0
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/minitest-5.10.2
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/mocha-1.2.1
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/msgpack-1.1.0
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/multi_json-1.12.1
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/multipart-post-2.0.0
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/net-scp-1.2.1
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/net-ssh-4.1.0
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/netrc-0.11.0
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/newrelic_rpm-4.2.0.334
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/nio4r-2.0.0
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/nokogiri-1.7.2
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/parallel-1.11.2
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/parser-2.4.0.0
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/paul_revere-2.1.0
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/pg-0.20.0
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/powerpack-0.1.1
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/public_suffix-2.0.5
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/rack-2.0.3
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/rack-attack-5.0.1
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/rack-test-0.6.3
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/rack-utf8_sanitizer-1.3.2
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/rails-5.0.3
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/rails-dom-testing-2.0.3
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/rails-erd-1.5.2
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/rails-html-sanitizer-1.0.3
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/rails-i18n-5.0.4
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/railties-5.0.3
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/rainbow-2.2.2
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/raindrops-0.18.0
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/rake-12.0.0
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/rbtrace-0.4.8
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/rdoc-5.1.0
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/rest-client-2.0.2
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/rubocop-0.49.1
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/ruby-graphviz-1.2.3
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/ruby-progressbar-1.8.1
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/sass-3.4.24
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/shoryuken-2.1.3
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/shoulda-3.5.0
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/shoulda-context-1.2.1
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/shoulda-matchers-2.8.0
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/sprockets-3.7.1
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/sprockets-rails-3.2.0
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/sshkit-1.13.1
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/statsd-instrument-2.1.2
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/thor-0.19.4
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/thread_safe-0.3.6
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/timers-4.1.2
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/toxiproxy-0.1.4
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/trollop-2.1.2
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/tzinfo-1.2.3
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/uglifier-3.2.0
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/unf-0.1.4
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/unf_ext-0.0.7.4
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/unicode-display_width-1.2.1
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/unicorn-5.3.0
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/validates_formatting_of-0.9.0
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/websocket-driver-0.6.5
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/websocket-extensions-0.1.2
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/will_paginate-3.1.5
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/xml-simple-1.1.5
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/xpath-2.0.0

Once we have that list of paths, we can combine it with a search tool. In this example, we're using rg, which is the ripgrep tool. Ripgrep is a search tool similar to grep, but optimized for source code. Finding places in our gems where the method create_or_update is defined is suddenly a breeze once we have Bundler and Ripgrep working together.

$ rg 'def create_or_update' $(bundle show --paths)
/Users/andre/.gem/ruby/2.3.3/gems/bundler-1.14.6/lib/gems/bundler-1.14.6: No such file or directory (os error 2)
/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/activerecord-5.0.3/lib/active_record/callbacks.rb
297:    def create_or_update(*) #:nodoc:

/Users/andre/src/rubygems/rubygems.org/.bundle/ruby/2.3.0/gems/activerecord-5.0.3/lib/active_record/persistence.rb
546:    def create_or_update(*args, &block)

bundler/inline for single-file scripts

We're starting to run out of time, but before we wrap up I want to highlight one more feature that offers developers a very powerful tool. Every tip so far has been about managing gems for an application. What about the times when an application is overkill, and you just want to write a few lines of code into a single file?

Ruby was originally created for that kind of small, helpful script, and makes it very easy... until your small script starts depending on gems. Then you have to think about installing them, making sure the right version is available, and all the other thing that Bundler was invented to help with. If you have a small script that could use some gems, Bundler can help with that as well. This feature is called 'inline Gemfiles', and it gives your single-file scripts superpowers.

At the top of your script, require bundler/inline. Then, use the gemfile method to declare your dependencies just like you would in a standalone file. When you run the script, Bundler will jump in and make sure the gems you need are installed and loaded, and your script will always be able to run successfully.

$ vim script.rb

require "bundler/inline"

gemfile do
  source "https://rubygems.org"
  gem "rack-obama"
end

puts "rack-obama's version is: #{Rack::Obama::VERSION}"

Once we've created a script that uses inline Gemfiles, just running it means Bundler will take care of everything else. Any missing gems are automatically installed, all installed gems are automatically used, and you never have to think about it. As you can see, we do not currently have the rack-obama gem installed on this machine.

    $ gem list rack-obama

*** LOCAL GEMS ***

Under normal circumstances, our script would fail with an error about a missing constant. Bundler is going to silently install our missing gem as part of running our script. Take a look:

$ ruby script.rb
rack-obama's version is: 0.1.1

Bundler made the script's dependencies work, completely automatically! If we check on installed gems again, we can see that Bundler installed the gems we needed exactly as if we had run gem install ourselves:

$ gem list rack-obama

*** LOCAL GEMS ***

rack-obama (0.1.1)

With that, it’s time to wrap things up! If you’re interested in the latest developments (ha) in Bundler, check out the Bundler blog at bundler.io/blog, or follow us on twitter at @bundlerio. We post and tweet about what changed anytime there’s a new release.

If you want to support development work and maintenance on Bundler, RubyGems, and RubyGems.org, check out Ruby Together. Our website is at rubytogether.org, and we're on twitter at @rubytogether.