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

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.

15 Responses to “TextMate Tip – The Ruby Bundle”

  1. on 14 Apr 2008 at 6:54 pmgaba

    Great introduction, thank you!

  2. [...] of the Ruby bundle, Ciarán Walsh has a post about the design philosophy behind the mnemonics of the Ruby bundle and a few [...]

  3. Ciarán Walsh’s Blog » TextMate Tip – The Ruby Bundle…

    Ciarán Walsh’s Blog » TextMate Tip – The Ruby Bundle…

  4. on 04 May 2008 at 7:35 amlinks for 2008-05-04 | Libin Pan

    [...] Ciarán Walsh’s Blog » TextMate Tip – The Ruby Bundle 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. (tags: textmate ruby bundle rails mac productivity) [...]

  5. [...] and TextMate: An interesting introduction to TextMate’s Ruby bundle. A good place to start if you use TextMate but haven’t used any of the mnemonics and snippets [...]

  6. [...] Edward Gray II has a nice article about the Ruby bundle for Textmate, the popular text editor for Mac OS X. If you are new to [...]

  7. [...] Ciarán Walsh’s Blog » TextMate Tip – The Ruby Bundle (tags: textmate ruby tips) [...]

  8. [...] TextMate Tip – The Ruby Bundle by Ciarán Walsh 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. [...]

  9. [...] just have to say: The more I learn about TextMate, Ruby, TextMate and Ruby together, or TextMate + Anything (Blogging for example), the more it blows my mind. I’ve [...]

  10. on 20 Sep 2008 at 9:08 pmBuddy

    When I try to run ruby code with the command R I get this:

    /Library/Application Support/TextMate/Bundles/Ruby.tmbundle/Support/RubyMate/runscript.rb:1:in `require’: no such file to load — /Applications/TextMate.app/Contents/SharedSupport/Support/lib/tm/executor (LoadError) from /Library/Application Support/TextMate/Bundles/Ruby.tmbundle/Support/RubyMate/runscript.rb:1

    Could anyone tell me what is wrong?

    Thanks

  11. [...] bin auf eine kurze Einführung in das TextMate Ruby Bundle gestoßen. Ich muss mir unbedingt die Triggers angewöhnen! Auch sehr lesenswert sind die 21 Ruby [...]

  12. on 24 Jan 2009 at 1:31 amJared Grippe

    I had this problem because i was using the Ruby.tmbundle from the SVN repo. looks like you also have a different version installed here /Library/Application Support/TextMate/Bundles/Ruby.tmbundle

    Try removing (or just moving) /Library/Application Support/TextMate/Bundles/Ruby.tmbundle and letting TM use the Ruby bundle it has in /Applications/TextMate.app/….

    that worked for me

  13. on 20 Nov 2009 at 5:24 amAjay

    What is this symbol

    on the keyboard for code completion. I see it everywhere in the bundle for code snippets.

    Thanks.

  14. on 22 Nov 2009 at 5:06 pmDon Smith

    13Ajay, that’s the Tab key.

    Ciarán, James, thanks for the great post.

  15. on 18 Jun 2010 at 3:00 ambennett

    trying out some of the Ruby Bundle but I’ve run into a problem… here’s the code I’m trying to run:

    class Song def initialize(name, artist, duration) @name = name @artist = artist @duration = duration end

    end

    song = Song.new(“Ruby Tuesday”, “Rolling Stones”, 360) song.inspect

    however when I use the “Run” command the output window shows up blank – no errors, just blank.

    I’m obviously a bit of a noob – can you tell me what’s going on??

    thanks b

Fork me on GitHub