Scripting with RVM
Due to the way RVM is written, rvm not only works from the command line but is also suitable for scripting. When writing a script that relies on using the rvm command line interface to switch rubies and / or gemsets, you need to keep in mind that there is one important distinction from normal use.
Please read and understand Binary vs Function Mode below.
Binary Mode vs Function Mode
RVM can be run in two different modes - by default, if the rvm binary is available in your path / is used with an absolute path, RVM will run as a binary. This means that rvm can do the vast majority of most operations (e.g. installing rubies, invoking set operations) but certain things (like switching the current ruby) will fail as it cannot change the environment it operates in.
When RVM is used as a shell function (by sourcing the scripts/rvm file, either in your shell profile files or, in the case of scripting, directly in your script, it gains the power to manipulate the environment it is run in - namely, rvm use and rvm gemset use operate as expected, switching the GEM_HOME, PATH and other assorted environment variables used for normal operation.
Since there are two modes that you can run RVM as, the first option you have is to run as a binary, using rvm do to fire one off commands inside specified environments.
For example, to create a gemset in any environment
rvm 1.8.7-p334 do rvm gemset create my_gemset
The second option (which is likely what you need) is to add a sourcing line, the same one you would put into your interactive login shell's profiles.
For example, for a user installed RVM
\# Load RVM into a shell session *as a function* if [[ -s "$HOME/.rvm/scripts/rvm" ]] ; then \# First try to load from a user install source "$HOME/.rvm/scripts/rvm" elif [[ -s "/usr/local/rvm/scripts/rvm" ]] ; then \# Then try to load from a root install source "/usr/local/rvm/scripts/rvm" else printf "ERROR: An RVM installation was not found.\n" fi
After the above code appears in a bash or zsh script, you will be able to use normal rvm commands that affect the environment in the remainder of the script.
If you wish to see if rvm is properly loaded as a shell function and not a binary you can insert the following line into your script.
type rvm | head -1
If the above line outputs 'rvm is a function' then everything is as it should be. If the outputs that "rvm is hashed" or "rvm is $rvm_path/bin/rvm" or even "not found", then it is running as a binary or not at all within the script and will not be able to control the environment of the script as expected.
If you're writing a script and you come across a case where the ruby isn't being 'correctly switched' inside of the script, then the rvm () function is not loaded into the shell and it is instead a binary.
For more information, see this gist