Build with your RVM’s Ruby in Sublime Text 2

by Jake on January 22, 2012

Unless you’ve been living under a rock, you’ve probably heard about Sublime Text 2. This app is a cross-platform (*nix, OSX and Windows) text editor, and is a successor to the Windows-only Sublime Text. Despite it currently being in beta, Sublime has tons of features that make it a powerful text editor.

One of the cool features in Sublime is the build function (TextMate has this as well). You can write a ruby script and then run it within the text editor. The result will print out in Sublime’s console. This can be handy for testing a script, or just fun to play with. You can also require files and run an entire application with it.

Another cool app for all of the rubyists out there is rvm. It’s a ruby version manager, giving you the ability to “hot swap” between different ruby installs. So if you have an app that requires 1.8, but you have 1.9.2, you can just type in rvm use 1.8 and your terminal session will switch to using 1.8. You can also manage multiple gemsets, helping keep things in order and nice and separate.

When I work with rvm, I like to set my default ruby to a version that I use all the time. In this case it’s 1.9.2. I only need to swap generally in special cases. The --default is great, but the problem with RVM is that it is loaded into your console via your profile. When Sublime runs it’s build script, it doesn’t look for your ~/.bash_profile, instead it loads your PATH’s ruby instead (in this case, /usr/bin/ruby).

The Problem

I want to get Sublime to load RVM before it runs ruby.

The Solution

Thanks to Robert, this post has been updated with a simpler solution. This should be compatible with Linux users.

Edit your build function

Navigate to your Sublime Packages folder, go to the Ruby folder and find the Ruby.sublime-build file. Edit it so that it is like this:

Ruby.sublime-build
1
2
3
4
5
{
    "cmd": ["HOME_DIRECTORY/.rvm/bin/rvm-auto-ruby", "$file"],
    "file_regex": "^(...*?):([0-9]*):?([0-9]*)",
    "selector": "source.ruby"
}

Note: you must specify the absolute path to your home folder. ~ does not work in this case.

If you’ve done everything right, you should be able to build your Ruby files now in your rvm’s default ruby.

BONUS: Use .rvmrc

When you run rvm-auto-ruby instead of ruby, rvm will use your –default ruby/gemset combo. It will also check for any .rvmrc files and use the ruby/gem combo specified in that file. If you require using a separate ruby/gemset combo from your default (e.g. jruby) you can add this to your projects’ root folder. The rvm official site provides excellent documentation on this feature and how to use it.

{ 6 comments… read them below or add one }

tigrish January 23, 2012 at 12:40 pm

Is there any way to load a specific gemset?

Reply

Jake January 23, 2012 at 6:02 pm

Good question. You could just append the rvm use your_ruby@your_gemset to the code as it’s just referencing RVM as you would in bash.

https://gist.github.com/1629627

EDIT: Robert provided an easier method to use this feature with .rvmrc.

Reply

babi4 January 23, 2012 at 12:30 pm

But what I should do, if prefer many .rvmrc files for each project?

Reply

Jake January 23, 2012 at 2:41 pm

rvm-ruby-head should pull in the first .rvmrc file it finds. Consider this file structure:

/root
    /app1
    /app2

Let’s say /app1 and /app2 use different rubies (for some reason). We’d add an .rvmrc into each folder specifying which ruby/gemset combo we’d like to use with the rvm --rvmrc my_ruby@my_gemset command.

Reply

Robert January 23, 2012 at 10:56 am

Why not just use ~/.rvm/bin/rvm-auto-ruby? It’ll setup whatever the project’s .rvmrc says to use.

Reply

Jake January 23, 2012 at 12:31 pm

You, sir, win the internets. I’ll update my post now.

Reply

Leave a Comment

{ 1 trackback }

Previous post:

Next post: