That’s easy, just make sure your code is saved as a .rb file and hit Command+B. But wait, there’s more…
Did you also know it’s possible to run your Ruby code straight from inside of Sublime Text 2… using RVM’s default Ruby?
There might be a better way but here’s one a quick and dirty for an OSX environment. If you are using Linux, it’s hopefully the same but with the file paths modified:
Cut a hole in a box.
Open `~/Library/Application Support/Sublime Text 2/Packages/Ruby/Ruby.sublime-build`
And replace it’s contents with the following (be sure to substitute your user name):
- Launch Sublime Text 2
- Open a ruby file
- Hit Command+B
- The Sublime Text console should pop up with your output. If you want it to go away, hit escape.
Want to prove it’s working?
Here’s a quick script that computes factorial and then prints out the result AND the ruby version using ruby’s constant “RUBY_VERSION”:
Drop that into your own factorial.rb, open it in Sublime Text, and hit Command+B. It should look something like this:
As you can see above, that’s 1.9.3… and as you can see below… that’s what my RVM is set to use as it’s default ruby.