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.