Skip to content

Instantly share code, notes, and snippets.

@divoxx
Created October 13, 2010 20:49
Show Gist options
  • Select an option

  • Save divoxx/624885 to your computer and use it in GitHub Desktop.

Select an option

Save divoxx/624885 to your computer and use it in GitHub Desktop.
# Iterate the directory tree up looking for a .rvmruby file.
# If it finds one, it executes rvm use ${content_of_.rvmruby}.
#
# This way you can easily automate ruby version switching for
# some projects.
rvm_set_ruby_for_path() {
orig_path=$(pwd)
until [[ -f .rvmruby || `pwd` == "/" ]]; do
builtin cd ..
done
if [[ `pwd` != "/" ]]; then
rvm use $(cat "`pwd`/.rvmruby")
fi
builtin cd "${orig_path}"
}
# Automatically change set rvm's ruby when directory changes.
cd() {
builtin cd $@ && rvm_set_ruby_for_path
}
Usage example:
$ echo "1.8.7@rails2" > Projects/Foo/.rvmruby
$ echo "1.9.2@rails3" > Projects/Bar/.rvmruby
$ cd Projects/Foo
Using /Users/divoxx/.rvm/gems/ruby-1.8.7-p302 with gemset rails2
$ cd ../Bar
Using /Users/divoxx/.rvm/gems/ruby-1.9.2-p0 with gemset rails3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment