2. Getting started

Make sure that you have RubyCocoa installed. You can find instructions for downloading and installing it here. You’ll also need Xcode, Apple’s IDE for software development.

In Xcode, use File->New Project… to create a new project. In the dialog that appears, select “Cocoa-Ruby Document-based Application” as the project type. Name the project “rubyrocks”.

The project tempate specifies that the application’s document class is MyDocument. Let’s change that to Game. In the Groups & Files pane on the left side of the project window, open the Classes and Resources groups. Make these changes:

  • Rename MyDocument.nib to Game.nib.
  • Rename MyDocument.rb to RubyRocks.rb (we’ll put all of our Ruby code in this file). In RubyRocks.rb, change the name of the MyDocument class to Game. Find the windowNibName function; change it to return Game.

Now build and run your project. You should get an error: an alert box should appear with the message “No document could be created.” This is because the application is still looking for a class named MyDocument. To change this, find the Targets group in your Xcode main project window. Click on the triangle to its left to expose the rubyrocks target. Double-click on the target icon to edit the target’s build properties. In the Document Types table, double-click the value in the Class column and change it from MyDocument to Game.

Now when you run your project, you should see a new window appear with the words “Your document contents here.” That’s your document view. To get it ready, we need to make some changes to Game.nib.

Open Game.nib from within Xcode. Interface Builder will open the file. In the document window, delete the “Your document contents here” message and replace it with a CustomView object from the palette.

Select the Window object in the Game.nib main view and then use Command-3 to inspect its size properties. Set its width and height to 800×600 and click the “lock” checkbox. Then go to the window inspector with Command-1 and uncheck the “Zoom (and resize)” box (see the image at right). This will give the game world a fixed size. Now size the CustomView object so that it fills the window.

Hang in there! In my opinion, this is the most tedious part of the whole tutorial, so don’t get discouraged. Cocoa and NeXTSTEP mavens like to talk about how great it is to use Interface Builder (and compared with their previous alternatives, they’re probably right). But like Carl Jung, most of them have never used Ruby.

With the CustomView still selected, use Command-6 to bring up its Identity inspector. Change the class to GameView and add an outlet to your GameView class named game.

In the main Interface Builder window, select the File’s Owner object and use Command-6 to verify that its class is Game. Connect the GameView’s game outlet to the File’s Owner object.

Save the nib and exit Interface Builder. Run the application. You should now get an empty window and a warning message in the run log:

Unknown class ‘GameView’ in nib file, using `NSView’ instead.

It’s time to create the GameView. And from here on, it’s all Ruby.

Did you find an error? Is something missing? Post your comment or suggestion below!

Comments (17) post
  1. Johan Fri Nov 03 10:04:56 +0000 2006

    “Change the document class from MyDocument to Game” Should be “Change the document class from MyDocument to RubyRocks”

  2. Tim Burks Fri Nov 03 14:25:40 +0000 2006

    This depends on what the document class is named in your Ruby code.  In the example that follows, it’s called “Game”.  Did you change it to “RubyRocks” in your version?

  3. solo Thu Dec 14 08:56:00 +0000 2006

    “Add an outlet to your GameView class named game.”

    What should the type of the outlet be? It defaults to id, but doesn’t seem to want to connect to anything. (“Connect” and “Revert” buttons are disabled)

  4. Tim Burks Thu Dec 14 11:01:22 +0000 2006

    If you’ve told IB about your game class, then you can set the outlet type to “Game”. Since you’re connecting it to the “File’s Owner” object, you’ll need to set the class of “File’s Owner” to “Game” (see the instructions above).

  5. Eirik Mikkelsen Wed Jan 17 13:45:51 +0000 2007

    Actually, I have the same trouble as solo. The “Connect” and “Revert” buttons are disabled for all outlets of GameView. What are we missing?

  6. Tim Burks Wed Jan 17 17:46:20 +0000 2007

    Maybe you haven’t set the class of the File’s Owner proxy in your nib. Make sure that it’s Game. If you still have trouble, there’s an Xcode project that you can download in chapter 6.

  7. Eirik Mikkelsen Fri Jan 19 11:56:24 +0000 2007

    I went ahead and downloaded that file, and everything works as expected. Still trying to wrap my mind around Xcode and Interface Builder, so I probably missed a step in the description. Anyway, this site is a fantastic resource – thank you for taking the time to make it!

  8. Manfred Plagmann Fri Mar 02 12:13:16 +0000 2007

    There seem to be some confusion regarding the connection of the custom view and file’s owner instance. In order to make the connection (and get an active ‘connect’ button) one has to the control-click the gameview and drag a connection to the ‘File’s Owner’ instance. The ‘connect’ button becomes active and by pressing it the outlets destination should read ‘Game’. Finished.

  9. danny Thu Jun 14 12:36:55 +0000 2007

    I’m getting lost right here: “Double-click on the target icon to edit the target’s build properties. Select Document Types under Info.plist Entries->Simple View. Change the document class from MyDocument to Game.”

    I double click on the icon next to rubyrocks under the target icon. Then I see tabs: General, Build, Rules, Properties, Comments. But I can’t find any place to change the document class.

  10. Derick Thu Jun 14 20:13:27 +0000 2007

    Danny I’m having the same problem….with XCode 2.4.1…wondering if the tutorial was written for an earlier version.

    I tried this instead: I double-clicked the Info.plist file to edit it and changed

    NSDocumentClass MyDocument

    to

    NSDocumentClass Game

    and now it’s giving a window with the text “Your document contents here” as expected.

  11. Derick Thu Jun 14 20:15:56 +0000 2007

    sorry the change should be

    <key>NSDocumentClass</key> <string>MyDocument</string> to <key>NSDocumentClass</key> <string>Game</string>
  12. Gary Tue Sep 25 18:24:01 +0000 2007

    @Danny: Yeah, the interface seems to have changed. What I did was double click on the icon next to rubyrocks under the target icon. Then, as you said, given: General, Build, Rules, Properties, and Comments, I chose Propertiess and looked at the lowest panel. There, I saw three places where MyDocument was listed. I changed these to Game. It worked then without further changes (even though MyDocument occurs in other places).

  13. Martin Wed Dec 26 15:23:05 +0000 2007

    I’m new to Xtool and experienced problems with connecting the game output. After reading Manfreds explanation I managed to do so but it took a while for me to figure it out. This is how to do it (an explanation for the less gifted (like me)): In the “instances”-tab in the main window double click the “window”. Then the word GameView will light up in the other window (the “window”-window). From there you can control-click GameView and drag a connection to the File’s owner. This will enable the connect-button.

  14. Luser Tue Jan 29 06:42:09 +0000 2008

    How do you rename MyDocument.nib in Xcode ? ...

  15. Patrick Thu Feb 07 17:10:49 +0000 2008

    I’m using XCode 3 and am getting lost:

    ” In the Classes view, find MyDocument (it’s a subclass of NSDocument) and rename it to Game.”

    I can’t find MyDocument in the Classes view. Any help appreciated!

  16. Michael Black Mon Feb 25 09:14:14 +0000 2008

    Patrick – I’ve updated this section of the tutorial to work with Xcode 3.

  17. Michael Black Mon Feb 25 09:24:35 +0000 2008
    Luser – to rename MyDocument.nib:
    • In the ‘Groups & Files’ section on the left side of the project window, expand the ‘Resources’ group.
    • Right-click (or Ctrl-click) on ‘MyDocument.nib’, and choose ‘Rename’.