M-x all-things-emacs

Package Faves: rcodetools

July 21st, 2007 by Ryan McGeary · 84 Comments

rcodetools-xmpfilter Rob recently pointed me to rcodetools and its included emacs integration. Specifically, I’ve only had the chance to play with xmpfilter, but so far, I’m very impressed. Let’s use it to annotate lines in a ruby buffer with intermediate results.


First, let’s install rcodetools. I am going to assume that you already have ruby and rubygems installed.

$ gem install rcodetools

(Prefix your install with sudo if necessary on your system.)

Rcodetools is now installed under [GEM_INSTALL_DIR]/gems/rcodetools- and there is good emacs setup documentation in README.emacs, but for what I’m about to demonstrate here, you only need to install rcodetools.el. Copy rcodetools.el to somewhere in your emacs load-path and require it.

(require 'rcodetools)

Capturing STDOUT

Suppose you have a ruby script in a buffer that looks like this:

3.times { puts "Hello World!" }

Run M-x xmp and now your buffer will look like this:

3.times { puts "Hello World!" }
# >> Hello World!
# >> Hello World!
# >> Hello World!

Code Annotation

You can sprinkle annotated results in your source code by adding # => markers at the end of lines to show their values. This comment marker can be added to the end of a line automatically by running comment-dwim twice (M-; M-;).

If you had a script that looked like this:

def fib(n)
  n < 2 ? n : fib(n - 1) + fib(n - 2)
fib(2)           # =>
fib(11)          # =>

After running M-x xmp, the last two lines of your buffer will look like this:

fib(2)           # => 1
fib(11)          # => 89


I find this package pretty cool and I wish I knew about it sooner. This is going to change the way I write and test my one-off ruby scripts.

Xmpfilter can also be used to generate Test::Unit assertions and RSpec expectations automatically, but I leave that as an exercise for the reader. Find more information on xmpfilter.

(Rcodetools also ships with Vim support, and TextMate users get bundled xmpfilter integration for free.)

Tags: faves · reviews · ruby

84 responses so far ↓

  • 1 Pierre // Sep 18, 2007 at 2:43 am

    I can’t seem to get xmp to work in Carbon Emacs, or to find any help on fixing it. Whenever I try the “xmp” command the current buffer is just completely cleared. I don’t get any errors or anything. Also it works perfectly with plain ol’ emacs from Terminal.

  • 2 Ryan McGeary // Sep 18, 2007 at 11:53 am

    Pierre, That’s strange as I use Carbon Emacs too. The only thing I might recommend is making sure you have the latest version of Carbon Emacs, and that you have ruby in your emacs path. I have this in my .emacs:

    (setq exec-path (cons "/opt/local/bin" exec-path))
    (setenv "PATH" (concat "/opt/local/bin:" (getenv "PATH")))

    My ruby installation is under /opt/local/bin because I use the MacPorts defaults. Adjust accordingly for your environment.

  • 3 Pierre // Sep 22, 2007 at 3:49 am

    Thank you so much Ryan. It works perfectly now. I’ve actually had trouble with more than just rcodetools, and this little addition to my .emacs file has seemed to fix them all.

  • 4 Tyrone // Nov 9, 2007 at 3:53 am

    Err, why does modifying the path like that fix the problem? I just had to do the same on my ubuntu machine but I had to add /usr/bin.

    Seems strange to me that emacs doesn’t have the path correct.


  • 5 Ryan McGeary // Nov 9, 2007 at 2:18 pm

    Tyrone, It depends on your OS, distribution of emacs, and how emacs was launched. It can only be as smart as the information it has available. For example, sometimes env variables are set at a system level, sometimes at a user level, and sometimes in other source scripts like .bashrc or .zshrc.

    To me, it sounds like a lot of work to cover all the bases for emacs, but I’m sure it’s possible. Please do investigate.

  • 6 Edward Garson // May 6, 2008 at 9:48 pm

    I had the same issue Pierre had after upgrading ruby. Re-running `ruby setup.rb ‘ from within the rcodetools directory fixed the problem. Hope this helps!