Have you ever wondered how Avdi gets those results-as-comments in his Ruby Tapas episodes? I did. And apparently it’s one of the most asked questions he receives. The answer is
xmpfilter and it comes with extensions for vi/Emacs.
But if you’re a Sublime Text hacker like myself and mildly allergic to vi/Emacs, can you get the same results? Why, yes you can: sublime-text-ruby-markers.
Follow the instructions for installing the plugin (basically
gem install rcodetools and install Ruby Markers from Package Control). Now, open a text file and type
2 + 2 # =>
Now hit Shift-Alt-U and the buffer will change to
2 + 2 # => 4
Yay! We’ve just evaluated our ruby expression inside Sublime Text!
If, like me, you use
rbenv (and you should), you may have encountered a number of problems…
xmpfilter. After installing
rcodetools, make sure you run
xmpfilter. Make sure you turn on the rbenv detection in the Ruby Markers settings
Still not working!. More than likely, the plugin is not detecting rbenv correctly. This can happen if you install rbenv using homebrew on the Mac (the plugin looks for rbenv in
~/.rbenv/bin/rbenv. Personally, I just removed my homebrew version and installed rbenv from Git)
Weird shit keeps happening: it’s probably not using the ruby version you expect. I couldn’t for the life of me figure out why Ruby Markers was looking for Ruby 1.9.3-p194 when I don’t even have that installed on my Mac… but I did have it installed on a Vagrant VM and that project, with the project specific .ruby-version file, was loaded in Sublime and seemed to be the default directory. By default, the Ruby Markers plugin doesn’t respect the current directory. I have a fork (and pull request) which changes the current directory to that of the current buffer so rbenv loads the ruby version you’re expecting.