Posts RSS Comments RSS 32 Posts and 1,017 Comments till now

Archive for February, 2008

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 of Class definition snippets, sor to get the sort() iterator, or sorb for sort_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(), and attr_accessor() are r, w, and rw respectively. You also get the each() iterator with the common abbreviation ea, which means you would use eal for each_line() or eawi for each_with_index().
  • Some snippets are based on fictional method names like map_with_index(), deep_copy(), or application_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 ⌃&gt; 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 ⌘R by 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 ⇧⌘R by 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 ⌃⇧⌘E by 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.

TextMate Tip – The HTML Bundle

I’m going to kick off a look at some of the bundles with an overview of the HTML bundle.

Continue Reading »

Terminal Plug-in Updates

10.5.2 was just released, apparently bringing with it some changes to Terminal (anyone know what they are?). This caused Terminal’s version number to change, and thus my plug-ins to fail, so I’ve updated the downloads to account for it. Note: if you already edited the plug-ins by hand to work in 10.5.2 you do not need to download them again, as that is the only change that has been made.


BlurminalOriginal Post

Terminal Tab SwitchingOriginal Post

TerminalColoursOriginal Post

TextMate Tip – Macros

I’ve not posted for far too long as I’ve been busy with the holidays, starting a new job and working on a project, so apologies for the downtime, but hopefully this marks the return of regular posting. I’ll also try to work through all the comments as I’m a bit behind, so if I’ve not replied to you then apologies again.

Today I’m going to give some information about using Macros. Macros are simply a set of actions which you can record and then play back whenever you like. Most actions, including text editing, find and replace, clipboard usage, commands and snippets will be recorded, but there are some which are not (switching files and moving to a symbol for example).

Continue Reading »

Fork me on GitHub