Scripting RVM

While working on an issue for the highgroove-generator tool today, I came across an issue with RVM I hadn’t faced before. The tool generates a new rails project setup just how we like it and needed to create an RVM gemset and run some commands using that gemset.

I used thor to build the command line tool for the generator, which made it easy to script out the commands needed to script out the commands needed to put together the project. However, because the commands are run in a separate subshell each there are some problems with using rvm create and rvm use as I normally would to setup a gemset.

Digging into the RVM documentation, I came across an action I hadn’t seen before: rvm do. The do action lets you specify a ruby version and/or a gemset on a per command basis without having to worry about the state of the shell’s environment. So using thor, run 'gem install bundler' becomes rvm 1.9.3-p125@my_app do gem install bundler. Viola!

Checkout my commit to the generator to see rvm do in action.