RVM (Ruby Version Manager) is Neat.

If you are working on Ruby then you are either probably using the default Ruby that came with your Mac or installed via apt-get or manually installed a version of Ruby. This is great, and can get you going pretty quickly but what if you need to use another version of Ruby, even temporarily? Ach, now you need to setup which version your system should use by default, where the gems are to be installed and maybe even have to grab the source and build the whole thing yourself. And that’s great, usually once you learn the basics of how to configure and setup the environment it isn’t too bad to repeat the process when needed. But what if you want to experiment with different installations? What if you’d like to easily test how your code behaves on different versions of Ruby? What if you want to have a private Ruby install configured and tailored just to the development piece you’re working on? That could become quite cumbersome and repetetive. Fortunately you don’t have to worry about that mess any longer, just use RVM.

RVM is short for Ruby Version Manager. It’s a gem that will help with grabbing, building and configuring pretty much any Ruby environment. Let’s see what we can do…

As a Ruby developer
I want to install the latest version of Ruby without destroying my environment

Scenario: I want to install JRuby
  Given I have a Ruby version installed
  And I have RubyGems installed
  And I have a Java JRE installed
  When I execute “gem install rvm”
  And I execute “rvm-install”
  And I execute “rvm install jruby”
  Then I should have jruby installed

Scenario: I want to switch to using JRuby as my default Ruby
  Given I have installed RVM
  And I have installed JRuby with RVM
  When I execute “rvm jruby –default”
  Then JRuby should be the default Ruby

If you’ve played with Cucumber the above syntax may be familiar. It’s just a description of what we can do with RVM and what we should expect. To see all the available versions of Ruby installed and and available to RVM simply type rvm list. The current default Ruby version will be highlighted with a => prefix. Switch back to the standard Ruby version just type rvm 1.8.7 –default. To get a list of known Rubies that you can install with RVM type rvm list known. Play around with variations of rvm –help or rvm <cmd> –help.

But RVM isn’t just about grabbing and installing different versions of Ruby. It can also be used to execute commands on different Ruby versions without touching your default environment.

Let’s create a Rakefile to test executing rake using different versions of Ruby without switching around our environment. Copy in the following text.

task :default do puts “Using Ruby version #{ENV['RUBY_VERSION']}.” end

Okay, let’s try running this rake file using JRuby.

Scenario: I want to execute my rake file with JRuby
  Given I have installed RVM
  And I have installed JRuby with RVM
  And I have the rake file on my current working directory
  When I execute “rvm jruby rake”
  Then I should see “Using Ruby version jruby-1.5.0.”

Cool, huh? Now let’s try doing that with Ruby 1.8.7…

Scenario: I want to execute my rake file with JRuby
  Given I have installed RVM
  And I have installed Ruby 1.8.7 with RVM
  And I have the rake file on my current working directory
  When I execute “rvm 1.8.7 rake”
  Then I should see “Using Ruby version ruby-1.8.7-p249.”

NEAT! Of course this can apply to any version of Ruby that you’ve configured with RVM.

This is just the tip of the iceburg for what you can do with RVM. It’s got a lot of features and functionality and some nice documentation.

To see the available options just type…

rvm help

This should give you just enough RVM knowledge to install and configure different Ruby versions and execute alternative Rubies without changing your current configuration. Enjoy.

Posted via email from mdh@just3ws.com