More intelligent .rvmrc files
One of the joys of using RVM is the use of .rvmrc files, which makes it much easier to ensure that you are always using the right ruby and gemset on a per project basis. It’s also best practice to check you .rvmrc file into version control, because it serves as an important piece of documentation when working in a team, namely “this is the ruby version this application uses”.
However, the problem with using this method – described in more detail on the RVM site – is that it assumes all the members on the team are using gemsets.
Some of the guys in my team are installing their gems in a .bundle folder in the application (not part of version control!) and therefore don’t need gemsets. Both approaches have their pros and cons, but we needed a more flexible way of dealing with this subtle difference which didn’t force the creation and use of gemsets.
Here’s our more flexible .rvmrc file…
ruby_version="1.9.2"
gemset_name="red-dragon"
ok="\033[10;32m"
warn="\033[1;31m"
reset="\033[0m"
if rvm list strings | grep -q "${ruby_version}" ; then
rvm use ${ruby_version}
if rvm gemset list | grep -q "${gemset_name}" ; then
echo -e "${ok}Using the ${gemset_name} gemset${reset}"
rvm gemset use ${gemset_name}
else
echo -e "${ok}The ${gemset_name} gemset does not exist, using global gemset${reset}"
fi
else
echo -e "${warn}You do not have the required Ruby installed: ${ruby_version}${reset}"
fi
All we do is check for the presence of the required ruby, then check for the expected gemset. If both are present then use them. Otherwise, just tell the user they don’t exist and leave it up to them to take the appropriate action.
When you go into the project directory in your console, you get this delightful message:
