Last Updated: February 25, 2016
·
4.23K
· destructuring

Hiding rvm, bundler from your users

If you recognise this way of running ruby scripts, read on:

cd project-dir # with Gemfile
rvm ree
bundle exec bin/some-ruby-script

It'd be nice to put that project-dir/bin into $PATH and just run some-ruby-script without finding a Gemfile, switching rubies with rvm, and typing bundle exec:

some-ruby-script

To get rid of rvm, create a ruby wrapper that ends up in the script's shebang:

rvm wrapper ree destructuring ruby gem irb bundle

This creates destructuring_ruby in rvm/bin but can be moved, renamed to anything. Shebangs now look like:

#!/usr/local/rvm/bin/destructuring_ruby

To get rid of bundle exec, jus use rubygems and bundler in your script:

require 'rubygems'
require 'bundler/setup'

To get rid of finding the Gemfile, set BUNDLE_GEMFILE. Finding the Gemfile of a project should work in production deploys, development workareas, and even when the bin/ dir is symlinked.

This is nasty but it works. Always looking for a way to improve it:

ENV['BUNDLE_GEMFILE'] = File.join(File.expand_path('../..', File.realpath(__FILE__)), 'Gemfile')

So the script skeleton looks like:

#!/usr/local/rvm/bin/destructuring_ruby

require 'rubygems'

ENV['BUNDLE_GEMFILE'] = File.join(File.expand_path('../..', File.realpath(__FILE__)), 'Gemfile')
require 'bundler/setup'