Using Ruby within TextMate snippets and commands

Posted by Dr Nic on June 11, 2008 and blessed with 8 comments

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

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

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

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

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

Related posts:

  1. Future proofing your Ruby code. Ruby 1.9.1 is coming. Bugger. I’m a Ruby monogamist. I use the Ruby...
  2. TextMate easter egg: find bundle commands by key combo I’ve dreamed of the ability to ask TextMate “what frigging...
  3. My attempt at sake task management I’ve used sake intermittently in my workflow. It competes...
  4. Unit Testing iPhone apps with Ruby: rbiphonetest Everything to love about Ruby: the concise, powerful language;...
  5. How to yell at people with GitHub from TextMate Sometimes when you are perusing code you ask the question:...
Trackbacks

Use this link to trackback from your own site.

Comments

Leave a response

  1. Will Leinweber Wed, 11 Jun 2008 12:16:15 UTC

    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. mixed's me2DAY Wed, 11 Jun 2008 15:23:37 UTC

    용우의 생각…

    ruby로 textmate의 snippets나 commands만들기….

  3. Hugh Evans Wed, 11 Jun 2008 20:50:17 UTC

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

  4. Dr Nic Wed, 11 Jun 2008 21:32:56 UTC

    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. Using Ruby within TextMate snippets and commands Wed, 18 Jun 2008 10:53:52 UTC

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

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

  7. TextMate Screencasts for Rubyists Thu, 19 Mar 2009 22:20:20 UTC

    [...] 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. Websites tagged "textmate" on Postsaver Sat, 08 Aug 2009 22:32:25 UTC

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

Comments