How to use Ruby in TextMate Snippets and Commands

Dr. Nic, one of our favorite Aussies (along with Ray Ozzy), posted a screencast on how to create TextMate snippets with Ruby embedded in them, as well as how to create commands with Ruby. Here’re the Cliff’s Notes versions of a snippet and command that will guess the class name you want to use based on the file name:

Snippet with Ruby

class ${1:`#!/usr/bin/env ruby
require 'rubygems'
require 'active_support'
puts ENV['TM_FILENAME'].gsub(/\.rb$/, '').camelize

Command with Ruby

#!/usr/bin/env ruby
require 'rubygems'
require "active_support"
puts <<-EOS
class ${1:#{ENV['TM_FILENAME'].gsub(/\.rb$/, '').camelize}}

He mapped these to “cla” in the bundle editor, but I recommend avoiding abbreviations. You’ll spend more time trying to remember how you abbreviated something than you’ll save by not typing two lousy characters.

The screencast’s volume is really low, so you’ll need to plug those external speakers into your laptop and crank the volume to what would normally be deafening levels just to hear him. The little tinny speakers on your MacBook won’t cut it this time.

Explore posts in the same categories: Ruby

Tags: , ,

You can comment below, or link to this permanent URL from your own site.

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: