Dr Nic

Using Ruby within TextMate snippets and commands

I didn’t know you could run Ruby within TextMate snippets. As a consequence, a lot of the TextMate bundles I work on either have simplistic Snippets or the advanced code is run via Commands with code pushed into explicit Ruby files in the Support folder.

But sometimes I just want a clever snippet. For example, I want the ‘cla’ snippet to use the current filename to create the default class name instead of the current ‘ClassName’ default. I want default foreign key names to be meaningful.

I’ve now figured this out (thanks to Ciaran Walsh), and …

Um, lost already? Ok, let me show you via screencast on Snippets and Commands with Ruby (QuickTime (11Mb)):

TextMate Snippets running Ruby from Dr Nic on Vimeo.

Want to learn more about living with TextMate and Ruby?

The TextMate website has a series of videos, including one by the Ruby.tmbundle’s own James Edward Gray II (JEG2).

In addition, there is the latest TextMate for Rails 2 Peepcode written by myself and spoken by Geoffrey Grosenbach. Its cheap at $9, good value at $15.50, and perhaps overpriced in the $20-$30 range. Lucky its only $9.

The snippets used throughout the video

The current Ruby.tmbundle snippet (activated via ‘cla’):

class ${1:ClassName}

An attempt to use regular expressions to convert the filename to a classname:

class ${1:${TM_FILENAME/[[:alpha:]]+|(_)/(?1::\u$0)/g}}

The final snippet, with embedded Ruby to do the heavy lifting (note: added ‘singluarize’ to the snippet):

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

Add this to your own Ruby.tmbundle, or clone mine (which is a clone of the original subversion repo).

Related posts:

  1. Packaging TextMate bundles in OS X DMGs Last week Engine Yard released a CLI for their Engine...
  2. Validate and Save your Ruby in TextMate – with secret Rubinus superpowers In some TextMate bundles, if you save a file it...
  3. Future proofing your Ruby code. Ruby 1.9.1 is coming. Bugger. I’m a Ruby monogamist. I use the Ruby...
  4. TextMate easter egg: find bundle commands by key combo I’ve dreamed of the ability to ask TextMate “what frigging...
  5. My attempt at sake task management I’ve used sake intermittently in my workflow. It competes...

8 Responses to “Using Ruby within TextMate snippets and commands”

  1. Thanks, good screencast. I never knew you could embed ruby in the snippits.

    Assuming that String#camalize doesn’t need a bunch of other stuff from ActiveSupport, it would probably be nicer to put that in some other file, so you don’t have to wait for rubygems and active support to be loaded while you’re waiting for the command to do something.

  2. 용우의 생각…

    rubyë¡œ textmate의 snippets나 commands만들기….

  3. Hugh Evans says:

    That’s pretty cool, although it does look a touch slow.

  4. Dr Nic says:

    I actually put singularize in the snippet, but forgot to include it in the demo so it does look unnecessary to use activesupport.

    In the rails bundle, we have our own inflection library to use. Not sure what to do within the Ruby bundle yet.

  5. [...] Dr Nic wrote Using Ruby within TextMate snippets and commands [...]

  6. [...] Using Ruby within TextMate snippets and commands [...]

  7. [...] Using Ruby within TextMate snippets and commands by Dr Nic Williams. Dr Nic leads us on a 7 minute journey into using Ruby within TextMate snippets. This is real power user stuff. [...]

  8. [...] – Using Ruby within TextMate snippets and commands saved by borisbez2009-08-01 – TextMate tip: Go to first non-space character on current line saved [...]