thewebdev.de

on software development & the web

test your Ruby app with various versions using rvm

05 Mar 2012

Recently I started to test my highscore library on Travis CI and they're using rvm on their test machines to easily switch the version and platforms. I only had MRI 1.9.3 on my local machine, but had some problems on 1.8.7. Removing 1.9.3 and installing 1.8.7 was no option, so I installed rvm (Ruby Version Manager) on my machine and I love it! So I am going to talk about it today, the benefits, going through the (easy) installation process and what you should be aware of.

First of all if you don't know it yet: there are several implementations of Ruby interpreters out there: the best known is MRI (Matz-Ruby-Interpreter), it's the first implementation and written in C, then there's Ruby Enterprise Edition, an improved version of the MRI. JRuby runs on the Java Virtual Machine, Rubinius is also written in C, but younger implementation, IronRuby runs on the .NET platform and MacRuby runs, well, only on Macs (who would've guessed it?).

Setup rvm

Installing rvm is a simple task, all you have to is that:

bash -s stable < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer)

This will install rvm in your $HOME directory and you should add the following line to your .bashrc (or in your .zshrc if you use the awesome zsh)

source $HOME/.rvm/scripts/rvm

Installing rubies

Now you have rvm on your system, but you don't have any Rubies in it. rvm list known will give you a list of all Rubies that are available. Start with installing the default Ruby, type rvm install 1.9.3 followed by rvm default 1.9.3. Now if you do ruby -v, you should see the 1.9.3 version. If not, start a new shell session, the rvm script must be loaded for the session first.

To switch to another Version or platform, you need the use command: rvm use jruby will switch to the latest JRuby version that you installed.

ruby gems

You already may have noticed that all the Rubies are installed in your $HOME directory, in the folder .rvm. All gems that you install will be put in this directory, there's a gems folder for every Ruby that you installed via rvm there. So you need to install gems for every version that you want to use.

Rvm has a lot of functions that go beyond the scope of the article, you could run rake tasks on multiple Ruby versions in one step, install a gem on multiple platforms, run your unit tests on 1.8.7 and 1.9.3 in one step (rvm 1.8.7,1.9.3 do rake task) and much more.
You should definitely check out the documentation.

Playing around with different interpreters and building libraries that run on many Ruby platforms is fun with that wonderful piece of software. Have fun!