RVM.el and Inf-Ruby (Emacs Reboot #14)

With my Emacs config files better organized, it’s time now to turn my attention to improving my experience editing Ruby files. First of all, I want to be able to quickly and easily drop into an IRB session from the code I’m working on.

First, I grab the rvm.el package from GitHub. This package is also available via ELPA, but the version in the archives is a little older and doesn’t work as well. I want the latest.

git submodule add https://github.com/senny/rvm.el.git elisp/external/rvm.el

I open up the rvm.el file, and byte-compile it with M-x byte-compile-file. Then I generate autoloads for it with M-x update-file-autoloads. I tell emacs to save the generated autoloads file as ~/.emacs24.d/init.d/rvm-loaddefs.el, so it will be picked up by my init-loading loop.

I integrate RVM into my Ruby editing experience by adding a new hook to ruby-mode-hook:

(add-hook 'ruby-mode-hook
          (lambda () (rvm-activate-corresponding-ruby)))

Now whenever I open a Ruby file, Emacs looks for the corresponding .rvmrc (if any) and reconfigures the environment to ensure that the appropriate Ruby executable will be used.

That done, I alter my “required packages” list to include the inf-ruby package, and then re-evaluate the code which installs the package list. Inf-Ruby stands for “Inferior Ruby”; it is a package which makes it possible to run an IRB session inside an Emacs buffer.

(setq abg-required-packages 
      (list 'xml-rpc 'magit 'gh 'inf-ruby))
; ...
(dolist (package abg-required-packages)
  (when (not (package-installed-p package))
    (package-install package)))

Now when I edit a Ruby file, at any time I can hit C-x C-s to drop into an IRB buffer and evaluate live Ruby code. rvm.el ensures that it is the correct Ruby version and gemset for the current project. inf-ruby has lots of handy keybindings for evaluating bits of Ruby files within the IRB buffer, so it’s easy to make a change to the code, send the new code to the inf-ruby buffer, and then play with the updated code without restarting the IRB session.

This post is part of the Emacs Reboot series. Check out the rest of the series for more Emacs goodness. You can also subscribe to a podcast feed of just the videos.