TextMate Tip – The Ruby Bundle
Today’s tip is a look at the Ruby bundle, which the bundle maintainer James Edward Gray II has kindly agreed to guest write. Take it away, James!
The Ruby bundle is a large collection of automations that will help you write and run Ruby code. If you are a Rails user, switch the language menu at the bottom of each editor window to "Ruby on Rails" to gain access to these automations plus those defined in the Rails bundle.
Writing Ruby Code
The Ruby bundle includes a large collection of snippet that simplify day to day coding in the language. At first glance through the menus, this may seem like an overwhelming amount of information to try and become familiar with. The truth is though that there are patterns to the snippets so that you can access what you need without having to memorize a bunch of triggers. The main things you need to know are:
- Snippet triggers for Ruby's flow control keywords are just the full name of the first keyword used:
if⇥,case⇥,while⇥, etc. - Other snippet triggers are usually the first three letters of the first word in the code you want, plus the first letter of any additional words. That means you can use
cla⇥to bring up a menu ofClassdefinition snippets,sor⇥to get thesort()iterator, orsorb⇥forsort_by(). - There are exception to the rule, mainly when the shortcuts can be made to read better. For example, the triggers for
attr_reader(),attr_writer(), andattr_accessor()arer⇥,w⇥, andrw⇥respectively. You also get theeach()iterator with the common abbreviationea⇥, which means you would useeal⇥foreach_line()oreawi⇥foreach_with_index(). - Some snippets are based on fictional method names like
map_with_index(),deep_copy(), orapplication_code(). These functional names tend to be easier to recall than the actual idioms they represent.
If this is the first time you are hearing of these mnemonics, practice a bit with my examples above to become familiar with them and then take a fresh look through the Ruby bundle menu. I suspect you will be surprised by how much you can retain without stressing your memory.
Don't be too surprised if some of the triggers are smarter than you expect. Some of them will add requires to your document (unless they are already present) to make sure needed libraries are available. If you want to bypass this feature for any reason, just tack an extra - onto the end of the trigger.
The bundle also contains several commands that can be useful in writing Ruby code. Here are two examples I use every day:
- All of the iterator snippets insert a block using the braces syntax, but you can quickly get to the do/end variation by tabbing into the body of the block and triggering the "Toggle 'do … end' / '{ … }'" command (it's on
⌃{by default). - Rails fans will appreciate the "Insert ERb's <% .. %> or <%= .. %>" command (on
⌃>by default). You actually use this tool in HTML files even though it's defined in the Ruby bundle, since that's by far the most common usage of ERb. Trigger it once to insert the tags or repeat the keystroke to cycle through the variations.
Finally, if you are addicted to Ruby, you may just enjoy the possibility to use a little of it when writing any TextMate document. The "Execute Line / Selection as Ruby" command (on ⌃⇧E by default) will hand-off any content you can select to the Ruby interpreter and replace the selection with the results of evaluating that Ruby expression. Aside from the neat possibilities of being able to generate code, or any other content for that matter, with code, this makes for a darn handy calculator at times.
Running Ruby Code
As soon as you have some Ruby code, you are going to want TextMate to run it for you. The Ruby bundle offers several ways to do that:
- The "Run" command (on
⌘Rby default) will run the code currently in the editor window in the RubyMate environment. This command has a lot of advantages over going to the command-line: output is shown in an HTML window with the standard TextMate styling; exceptions are captured by TextMate and formatted to stand out; Test::Unit files are recognized and receive a modified path that helps them locate other files; test failures and errors are hyperlinked back to the code in TextMate; and STDIN is modified to prompt with a GUI dialog when a line of input is required. - While you can handle full test files with the above, the "Run Focused Unit Test" command (on
⇧⌘Rby default) will run just the test method your caret is currently inside of. This can be quite a time saver for spot-checking changes. - Another tool definitely worth knowing is the "Execute and Update '# =>' Markers" command (on
⌃⇧⌘Eby default). To use it, add some comment markers (there is a snippet on#⇥that will insert these for you) to the end of lines you would like to see the results of and then trigger the command. TextMate will run your code and report the result of the marked line inline in the comments. This feature is great for code posted online since it shows the source and the results together.
There's more inside the Ruby bundle, but I've tried to hit the highlights that any Rubyist can probably benefit from knowing. You can get a lot of mileage out of knowing just this much.
Thanks a lot to James for writing this post, I hope you learned something. If you’ve still got a craving for more TextMate learning, James is also the author of TextMate: Power Editing for the Mac, which explains all about the robot ninjas hidden under TextMate’s shell and how to bend them to your will.
Ciarán :: Feb.20.2008 :: TextMate, Tips :: 17 Comments »