- loading...
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}
$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:
- Future proofing your Ruby code. Ruby 1.9.1 is coming. Bugger. I’m a Ruby monogamist. I use the Ruby...
- TextMate easter egg: find bundle commands by key combo I’ve dreamed of the ability to ask TextMate “what frigging...
- My attempt at sake task management I’ve used sake intermittently in my workflow. It competes...
- Unit Testing iPhone apps with Ruby: rbiphonetest Everything to love about Ruby: the concise, powerful language;...
- How to yell at people with GitHub from TextMate Sometimes when you are perusing code you ask the question:...
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.
용우의 생각…
ruby로 textmate의 snippets나 commands만들기….
That’s pretty cool, although it does look a touch slow.
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.
[...] Dr Nic wrote Using Ruby within TextMate snippets and commands [...]
[...] Using Ruby within TextMate snippets and commands [...]
[...] 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. [...]
[...] – Using Ruby within TextMate snippets and commands saved by borisbez2009-08-01 – TextMate tip: Go to first non-space character on current line saved [...]