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:
|
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.
{ 7 comments }

