-
Notifications
You must be signed in to change notification settings - Fork 626
Ruby
Here is another approach that just uses direnv and ruby-install to manage ruby projects.
Add this to the ~/.config/direnv/direnvrc
use_ruby() {
local ruby_root=$HOME/.rubies/$1
load_prefix "$ruby_root"
layout_ruby
}
Install ruby-install (brew install ruby-install
) and install a bunch of rubies.
ruby-install ruby 1.9.3
ruby-install ruby 2.0.0
ruby-install ruby 2.2.0
And then make a couple of symlinks for convenience:
cd $HOME/.rubies
ln -s ruby-1.9.3-p* 1.9
ln -s ruby-2.0.0 2.0
ln -s ruby-2.2.0 2.2
And finally in any project's .envrc
:
use ruby 2.0
This will put all gems under the project's .direnv/ruby
directory (makes opening gems easier). bundler will put wrapper binaries in .direnv/bin
(no more bundle exec
!).
It's also possible to use rbenv by adding the use rbenv
command in any .envrc
file. This will activate rbenv which in turn will put the ruby wrappers in the PATH.
Note that it's not necessary to install rbenv in the .bashrc or .zshrc for this to work.
Here is the most complicated .envrc that I use on ruby projects:
rvm use 1.8.7
layout ruby
PATH_add .direnv/bundler-bin
rvm is used to select the right ruby version for you
layout commands automatically set some of the usual environment variables. For now only the ruby layout exists. What it does is set the GEM_HOME environment variable and it's bin directory to your path. Because it depends on the ruby version, make sure to call it after "rvm". Since each ruby layout directories have their own GEM_HOME, you don't need to use rvm's gemsets.
PATH_add prepends and expands the given relative path. In that case, I use this to segregate the bundler binstubs from my own bin scripts with bundle install --binstubs .direnv/bundler-bin
If you want to find out what those commands exactly do, for now: cat direnv stdlib
| less
Add use_chruby
and the functions from Find Up with Alternates to your direnvrc
:
use_chruby()
{
local version
version="${1}"
[[ "${version}" == --auto ]] && version="$(read_version_file .ruby-version)"
[[ -z "${version}" ]] && return
local chruby
if has brew; then
local brew_prefix
brew_prefix="$(brew --prefix)"
if [[ -e "${brew_prefix}/opt/chruby/share/chruby/chruby.sh" ]]; then
chruby="${brew_prefix}/opt/chruby/share/chruby/chruby.sh"
fi
fi
[[ -z "${chruby}" ]] && [[ -e /usr/local/share/chruby/chruby.sh ]] &&
chruby=/usr/local/share/chruby/chruby.sh
[[ -z "${chruby}" ]] && return
source "${chruby}"
chruby "${version}"
}
With either use chruby 2.6
or use chruby --auto
, this will use postmodern/chruby installed in the usual places.
Instead of the default ruby layout you can use gem-home to manage it.
# Usage: layout ruby
#
# Sets the GEM_HOME environment variable to ".gem/ruby/RUBY_VERSION".
#
# Important: Use it AFTER selecting the ruby version!
layout_ruby() {
source /usr/local/share/gem_home/gem_home.sh
gem_home .
export BUNDLE_BIN=$(direnv_layout_dir)/bin
PATH_add "$BUNDLE_BIN"
}
To install gem_home do:
version="0.1.0" wget -O gem_home-0.1.0.tar.gz https://github.com/postmodern/gem_home/archive/v$version.tar.gz &&
wget https://raw.github.com/postmodern/gem_home/master/pkg/gem_home-$version.tar.gz.asc &&
gpg --verify gem_home-$version.tar.gz.asc gem_home-$version.tar.gz &&
tar -xzvf gem_home-$version.tar.gz &&
cd gem_home-$version/ &&
sudo make install