Nithin Bekal About

Defining gem dependencies in single file Ruby scripts

15 Jul 2020

Sometimes I come across cases where I need to write a simple script which uses a gem. The simplest way to go about this is to gem install gem-name and then require 'gem-name' in your script. However, this means that you need to remember to install the gem before running the script.

Bundler has an inline gemfile definition feature that lets you define gem dependencies in a single-file Ruby script. To use this feature, require 'bundler/inline' and provide a gemfile block that has the code that you normally put in a Gemfile.

require 'bundler/inline'

gemfile do
  source 'https://rubygems.org'
  gem 'benchmark-ips', require: 'benchmark/ips'
end

Benchmark.ips do |x|
  x.report('original') { naive_implementation() }
  x.report('optimized') { fast_implementation() }

  x.compare!
end

Now running ruby myfile.rb will automatically install the gem and run the script.

I always have a script like the above one handy. When suggesting a performance optimization when reviewing some code, it’s useful to be able to share a script like that. This lets the PR author to easily copy and tweak it without having to worry about installing the necessary gems.

Hi, I’m Nithin! This is my blog about programming. Ruby is my programming language of choice and the topic of most of my articles here, but I occasionally also write about Elixir, and sometimes about the books I read. You can use the atom feed if you wish to subscribe to this blog or follow me on Mastodon.