Pimp out your rvm profile

We’ve been using RVM for all our Rails projects here at Blenderbox for some time and we’ve made it a whole lot easier to use by adding this .rvmrc file to the root directory of all our projects.

#!/usr/bin/env bash

ruby_string="ruby-1.9.2"
gemset_name="gemset-for-project"

if rvm list strings | grep -q "${ruby_string}" ; then

  # Load or create the specified environment
  if [[ -d "${rvm_path:-$HOME/.rvm}/environments" \
    && -s "${rvm_path:-$HOME/.rvm}/environments/${ruby_string}@${gemset_name}" ]] ; then
    \. "${rvm_path:-$HOME/.rvm}/environments/${ruby_string}@${gemset_name}"
  else
    rvm --create  "${ruby_string}@${gemset_name}"
  fi

else

  # Notify the user to install the desired interpreter before proceeding.
  echo "${ruby_string} was not found, please run 'rvm install ${ruby_string}' and then cd back into the project directory."

fi
# Ensure that Bundler is installed, install it if it is not.
if ! command -v bundle ; then
	printf "The rubygem 'bundler' is not installed, installing it now.\n"
	gem install bundler
fi

What this little beauty does is looks for a ruby version and gemset that you set in the script and switches to it.  If it doesn’t exist but you have the ruby version installed, it will create a gemset for you and automatically switch to it.  If you don’t have that ruby version installed, it prompts you to install it before it creates the gemset.

It’s pretty awesome, so I hope you give it a try.  Once you do, you’ll never know how you lived with out it!  I’ve even used it for a few django projects to automatically switch to the right virtualenv and start up tmux.

Comments are closed.