Posted tagged ‘TextMate’

How to use Ruby in TextMate Snippets and Commands

July 25, 2008

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
`}
$0
end

Command with Ruby

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

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.

Advertisements