RVM is a very useful tool for working with multiple Ruby interpreters, and it’s especially handy for testing libraries against multiple interpreters. Unfortunately (for me), it only works with bash, zsh, and similar shells, and I use tcsh—but I’ve found a workaround.

I suspect that very few people use tcsh these days, and that the proportion of Ruby programmers is lower still. But I do, for reasons that I’ll have to write about another time.

A comment by chetstone pointed me in the direction of a solution. tcsh doesn’t have functions, so it’s not possible to modify the current environment in quite the same way as RVM does in bash, but it’s easy enough to use RVM to modify a bash environment and then load tcsh within it. You only need to do this for certain commands, such as rvm use. Here’s how to do it:

First, install RVM:

> curl -Lo rvm-install.sh https://rvm.beginrescueend.com/install/rvm
> bash rvm-install.sh

Next, create an rvm.tcsh wrapper script somewhere in your path, and make it executable:

#!/usr/bin/env tcsh

set rvm_command="source ${HOME}/.rvm/scripts/rvm; rvm $*"

if ($1 == "use") then
  bash -c "$rvm_command && tcsh"
else
  bash -c "$rvm_command"
endif

Finally, add an alias to ~/.tcshrc:

alias rvm rvm.tcsh

From now on, when you type rvm in tcsh, it will pass that command to the real RVM. If the command starts with rvm use, you’ll be dropped into a new tcsh session using the specified RVM environment.

I’ve added annotations to my prompt to tell me if I’m in an RVM sub-shell (also in ~/.tcshrc):

set prompt_info = ""
if ($?RUBY_VERSION) then
  set prompt_info = "[$RUBY_VERSION] $prompt_info"
endif
# plus any other information you want

set prompt = "$prompt_info%. %# "
# yours may be more colourful

You can do all the normal kind of operations:

~ > ruby -v
ruby 1.8.7 (2010-06-23 patchlevel 299) [x86_64-linux]
~ > rvm install 1.9.2
Installing Ruby from source to: /home/paul/.rvm/rubies/ruby-1.9.2-p180,
this may take a while depending on your cpu(s)...
…
Install of ruby-1.9.2-p180 - #complete
~ > rvm use 1.9.2
Using /home/paul/.rvm/gems/ruby-1.9.2-p180
[ruby-1.9.2-p180] ~ > ruby -v
ruby 1.9.2p180 (2011-02-18 revision 30909) [x86_64-linux]

It’s a simple wrapper, but that’s all that’s needed to use most of RVM’s functionality.