MacRuby notes series: code snippets in Xcode 4

If you use TextMate’s “code snippets” feature to insert commonly-used templates of Ruby code, you’ll be pleased to see that Xcode has a similar facility for MacRuby. It comes pre-populated with a variety of Objective-C snippets, but it’s easy to add your own Ruby snippets.

If you read my article on Beautiful standardized RDoc comments for your Ruby / Rails methods you’ll know that I’ve standardized on this method commenting pattern:

  # Provides a human-readable size of the attachment
  #
  # * *Args*    :
  #   - +precision+ -> the precision of the returned value, defaults to 1
  # * *Returns* :
  #   - a size value such as "5.1 KB"
  # * *Raises* :
  #   - +RuntimeError+ -> if the body is nil
  def human_size(precision=1)
    #...

In TextMate I use “rd <TAB>” to insert the comment snippet, and “defc ” to insert a commented blank method. In Xcode I do the same; here’s how.

  1. Activate the right-hand Utilities drawer and in the bottom of it, click on the curly braces {} to activate the Code Snippet Library.
  2. Type or paste in the code that you want to turn into a snippet; highlight it all.
  3. Drag the highlighted code to the Snippet library. (It can be stubborn and not want to drag. Holding the mouse button down for a moment before dragging seems to help.)
  4. Your snippet has been added to the Library; click once on the snippet and a callout window will show you the snippet and permit you to edit it.
  5. Add your own descripton, completion shortcut, etc. Any text you surround with <# #> marks will be highlighted with a blue bubble for quick tabbing and substitution.

Here’s the full snippet.

    #
    #
    # * *Args*    :
    #   - ++ ->
    # * *Returns* :
    #   -
    # * *Raises* :
    #   - ++ ->
    #
    def

    end
Advertisements

5 thoughts on “MacRuby notes series: code snippets in Xcode 4

  1. I tried creating a def method snippet. Your instructions were very helpful. I am not sure why it didn’t indent the ‘end’ and code body properly. Is there a trick?

    class Yzzzz
    def method
    code
    end

    end

    Not what I was expecting.

  2. The syntax highlighting and ability to customize/enhance seems really lacking in 4.0. Does anyone know how to make 4.0 more aware of Ruby?

    • You’re right. While there’s a modicum of syntax highlighting, Xcode isn’t as Ruby-aware as it could be. I’ve searched for solutions and the general consensus is, the syntax highlighting is not within our end-user control. It seems that we’re going to have to wait for Apple itself to improve things for us.

      • There is a File>Open with External Editor option which allows you to use VIM, textmate, Emacs etc. The Xcode window updates as the external editor saves code. Just an option if you want more syntax highlighting and don’t want to wait for apple.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s