Bundler Inline Gemfile Dependencies

If you haven't noticed, the Bundler team has been on a tear lately. It's hard to keep up with the pace of the 1.10.x releases. Along with improvements, they have been adding features. One that you might have missed is the new bundler/inline and it's gemfile method.

Bundler Inline allows you declare your gem dependencies "inline" instead of a Gemfile. This is perfect for single file scripts. Everything can be contained in a single file, no Gemfile, no bundle install, and you can run your script without a prefixing it with bundle exec.

Recently at RubyLoCo Hack Night, we were challenged with creating an ASCII flag. Given a starter script that printed out a simple ASCII art American flag, our first thought was to print it in color. There is a gem for that named colorize. But how do we install the gem and require it in our script? We wouldn't be so pedestrian as to actually run gem install colorize. We are proud bundlers.

Default Flag

Inline Gemfile

For a simple script, creating a Gemfile is too much ceremony. The new Bundler Inline will help us. We require bundler/inline and then pass a block to gemfile. Inside the block, we use the traditional Bundler DSL.

The gems declared inside the gemfile block will automatically be required into our script. You can still pass the option to require: false. At the top of our script, we declare our gemfile inline:

#!/usr/bin/env ruby
require 'bundler/inline'

gemfile true do
 source 'https://rubygems.org'
 gem 'colorize'
end

By default, running your script will not install gems to your system. But, we can pass "true" as the first argument to check and install gems when our script is run.

Because this is all inline, we do not need to prefix our command line with bundle exec. We can just run our script, Bundler will install the required gems and require them for us. Now that we have required the colorize gem, our strings have color changing methods like:

puts '* * * * * * '.white.on_blue
puts '0000000000000000000000000000000000000000000000000000000'.white.on_white
puts '1111111111111111111111111111111111111111111111111111111'.red.on_red

And now... With the help of Bundler... And colorize... We present Happy Birthday America!

Colored Flag

What did we accomplish?

We started with an example script that printed an American flag in black and white. We wanted to make it red, white and blue with the colorize gem. We did not want to add a Gemfile to our directory for this single file script. We declared our gem dependencies inline using the new bundler/inline. Now, our gems will install when we run the script.

Final Script

#!/bin/env ruby

#!/usr/bin/env ruby
require 'bundler/inline'

gemfile true do
 source 'https://rubygems.org'
 gem 'colorize'
end

puts '* * * * * * '.white.on_blue + '1111111111111111111111111111111111111111111'.red.on_red
puts ' * * * * *  ' + '0000000000000000000000000000000000000000000'.white.on_white
puts '* * * * * * ' + '1111111111111111111111111111111111111111111'.red.on_red
puts ' * * * * *  ' + '0000000000000000000000000000000000000000000'.white.on_white
puts '* * * * * * ' + '1111111111111111111111111111111111111111111'.red.on_red
puts ' * * * * *  ' + '0000000000000000000000000000000000000000000'.white.on_white
puts '* * * * * * ' + '1111111111111111111111111111111111111111111'.red.on_red
puts '0000000000000000000000000000000000000000000000000000000'.white.on_white
puts '1111111111111111111111111111111111111111111111111111111'.red.on_red
puts '0000000000000000000000000000000000000000000000000000000'.white.on_white
puts '1111111111111111111111111111111111111111111111111111111'.red.on_red
puts '0000000000000000000000000000000000000000000000000000000'..white.on_white
puts '1111111111111111111111111111111111111111111111111111111'.red.on_red

CustomInk is a proud supporter of Ruby Together


by Chris Mar