Ruby Idioms Bundle for TextMate
[Update: An improved version of Ruby Idioms has been merged into the Ruby bundle in TextMate. Everyone gets Ruby Idioms with TextMate now.]
If you are a TextMate user (a Mac OS X text editor), we have something in common and you might be interested in the rest of this post. If not, skip it.
I have made my Ruby Idioms bundle available for anyone who would like to download it and try it out. This bundle contains all the Ruby shortcuts I use to program everyday. I've spent a lot of energy over the last six months refining them down to the keystroke, so hopefully there is a lot of value hidden in there.
If you just want to get to the goods and start playing, click the above link and run. However, if you want some insight into my thinking with these shortcuts, read on…
I'm a Unix guy, so I'm very comfortable with two or three letter triggers and that's what you are going to find here. Sometimes I use a one or two caracter trigger (r, w, and rw just make sense for
attr_accessor, for example), but three is the norm.
I mention this because it is not the TextMate style and may be a turn off to some.
All the core and many standard iterators are included in this bundle. I do mine differently from the normal TextMate style though, so I better mention that too. I find myself skipping around the object when it is included in the block more often than not (and I know where the escape key is when I do need the object), so I just want an empty block that leaves the snippet when I enter it (in case I need new triggers). That's my style.
I've included a compete set of test snippets in this bundle. It duplicates some work in the Rails bundle, but my opinion is that those tests are misplaced there anyway.
Some of my macros are really just snippets that ensure you have the proper require statement for the code. This can cause TextMate to flash the syntax highlighting because I have to replace all the code up to the cursor. If you would like to use the snippet, without the magic requires, just add a
- to the end of the tab trigger.
I use magic requires only where they make sense. For example,
def_delegator doesn't need one, because it's
extend Forwardable's job to handle that.
You will find some imaginary methods as snippet names, like
flatten_once. I use them because they are easy to remember names for more complicated idioms. I feel this is more natural, but some may be surprised when the inserted code doesn't look like the snippet title. Don't lose too much sleep over it, I say.
That Personal Touch
I wasn't kidding when I said these shortcuts are tuned to the keystroke. I've refined them after a lot of actual usage for what works best for me. You'll notice that extra tab stop in
unix_filter I leave in just to activate paragraph mode when I need it and that deleting
OTHER_ARGS in the option parsing code also removes the extra space. There are larger scale preferences too, of course.
assert needs a failure message, because it could be anything, but
assert_equal does not because the failure message makes sense from the method itself.
If you think like I do or can at least get into the flow, this should become pretty comfortable to work with. If not, it may just become a hellish set of magic rules you can't understand or remember. If the lattar is true, you may have to resort to you own bundle of tricks, but perhaps this will at least give you some ideas.