Sunday, May 1, 2011

rvm/bash Hack: Hop Into Gems Directory

I love rvm, but there's one thing I miss: I used to have an env var which enabled me to hop into my $GEMS directory and edit or read gems code. Because rvm puts gems into different directories, specific to the Ruby they're installed to or indeed the gemset, you have to manually navigate there on a gemset-by-gemset basis. Typing this out manually is a bit of a heinous pain in the ass, so I fixed it.

Put this simple bash function in your profile and you can easily hop into the gems directory for whichever gemset you're in. Caveat: it assumes you're using a gemset, and won't work for a bare, un-gemsetted Ruby or for system Ruby.

Update: Foca uses a similar hack, but it runs on gem env gemdir, which'll work under any Ruby, rvm-ed or not. And CainĂ£ Costa told me you can use bundle open #{gem_name}, which only requires setting $EDITOR or $BUNDLER_EDITOR environment variables. That's actually best for TextMate, but workable in vim and I'm assuming standard in emacs, which seems more like a shell than a text editor anyway.