2. Elements of RubyCocoa
2.1 Ruby
- Mark Slagell and Matz’s Ruby user’s guide.
- The online first edition of Programming Ruby: The Pragmatic Programmer’s Guide.
- The printed second edition of Programming Ruby. Because there’s a picture of a pickaxe on the cover, this and its predecessor are commonly called “the pickaxe book”.
Ruby is a powerful object-oriented scripting language that has been rapidly growing in popularity. It was developed by Yukihiro “Matz” Matsumoto in the early 90’s and has a vibrant community of users and enthusiasts.
As an increasingly grey-haired software developer, I’ve found some things in Ruby that make me like it a lot:
- Everything is an object. No more having to remember what’s an object and what’s a built-in type (as in Python). Everything can accept messages, which makes code easier to write and more consistent overall.
- All constructs have values. As above, no more remembering whether some specific function returns a value or not. This also allows us to write nice tight expressions like this:
myList.sort.uniq.each {|x| puts x.length}
- I can interact with Ruby and the programs I write in Ruby using irb, the interactive Ruby console.
- Ruby is introspective. At runtime I can easily ask objects their type and what messages they accept. In fact, because of this capability, the type of an object is of secondary importance: what matters most is the set of messages that an object can handle. Ruby documentation author Dave Thomas called this “duck typing”, because “if it walks like a duck, and talks like a duck, then it might as well be a duck.”
- Ruby makes it easy to add domain-specific extensions. This means that I don’t have to write parsers for custom languages and don’t need to use special-purpose command languages like Tcl (which have a tendency to “creep up” and turn into programming languages when users start writing 10,000 lines of customization!)
- I can easily extend existing classes. Ruby classes are open; at any time or place a Ruby program can add a new declaration of an existing class that adds or modifies its methods. This provides programmers with an easy way to add new convenience methods to classes and to work around problems quickly.
- I can use Ruby for nearly everything: builds, web programming, application programming, system programming. The Ruby community is bustling with activity; people are applying Ruby to new problems all the time and a lot of code is being shared.
And the reason I started using RubyCocoa:
- Using RubyCocoa and Objective-C, I can easily mix Ruby and compiled C and C++ code.
2.2 Objective-C
Objective-C is an object-oriented extension of C that is derived from SmallTalk. It was developed by Brad Cox in the early 1980’s around the same time that C++ was becoming popular.
The best way to get started with Objective-C is to get a copy of Andrew Duncan’s Objective-C Pocket Reference from O’Reilly and read it cover to cover. At 122 pages, it will take an afternoon, but it’s completeness and brevity will deepen your appreciation of Objective-C, especially if you’re an experienced C++ user. You’ll also find that it’s an easy-to-use reference when you need help recalling forgotten details.
Apple also provides an Introduction to The Objective-C Programming Language on it’s Developer Connection website. It’s more verbose than Duncan’s guide, which may get tedious if you’re an experienced developer, but it’s worth reading at least once.
For an in-depth reference on the amazing level of detail that the Objective-C runtime provides, see Apple’s Objective-C Runtime Reference.
Both Objective-C and C++ added object-oriented features to C, but did so in very different ways. C++ emphasized static type-safety: objects were defined with fixed types and the correctness of their interactions was checked during program compilation. As a result, after a program was compiled, little information about the types of objects was kept—all that was needed was implicit in the compiled object code. In contrast, Objective-C was designed to be a dynamically-typed language in which the types of objects could vary at runtime. This made it necessary for compiled programs to keep a lot of information about the types of objects, most notably the messages that objects accept.
An Objective-C message is similar to a function call but more flexible. When a message is sent to an Objective-C object, the receiver may or may not be able to respond to it. If it is an unsupported message, an object may have a generic handler that can forward the message to another object, sometimes called its delegate. To support this and other dynamic capabilities, compiled Objective-C programs contain tables of type information that can be used to inquire whether or not certain messages are supported, to list the messages that may be sent to an object, and even to dynamically add support for additional messages. As we will see, these tables make Objective-C an ideal companion to Ruby.
...and yes, there is an Objective-C++
Because Objective-C and C++ have such different approaches to object-oriented programming, the extensions they make to C can be easily combined into a single compiled language, Objective-C++. There’s not a lot of reasons for wanting to mix features of the two languages, but there’s one very big one: developers often want to use libraries or frameworks developed in both languages in a single application. Objective-C++ is supported by a custom version of the gcc compiler shipped with Apple’s developer tools, and at Apple’s 2005 Worldwide Developer Conference it was announced that the changes to support Objective-C++ had been merged into the official gcc source code and would be part of the gcc 4.1 release. For more detail, see Apple’s Objective-C++ Release Notes.
2.3 Cocoa
Cocoa is a framework for building desktop applications. It was originally developed by NeXT Computer, Inc. in the 1990’s and is currently available for computers running Mac OS X. It is built on Objective-C and provides a rich collection of objects that correspond to user interface elements such as windows, views, and controls.
Cocoa is used by the majority of the OS X applications shipped by Apple and its third-party developers. Virtually every beautiful user interface feature that you see in Macintosh applications is directly available through the Cocoa interfaces.
Need to learn more about Cocoa?
My first recommendation is to get and work through Aaron Hillegass’ Cocoa Programming for Mac OS X. It’s a tutorial introduction that grew from Aaron’s many years of experience teaching Cocoa (and its precursor NeXTSTEP).
Then when you’re ready for a more conceptual view, I recommend the phenomenal Cocoa Programming by Scott Anguish, Erik Buck, and Donald Yacktman. This is a massive work that explains Cocoa in great detail. It also includes a helpful appendix on the Objective-C runtime (that appendix helped me make and submit my first bug fix for RubyCocoa!).
For something shorter, try O’Reilly’s Cocoa in a Nutshell. Part I of the book (150 pages) contains a well-written description of Cocoa and its components. But that’s unfortunately followed by a superfluous 300-page API reference that is essentially a list of Cocoa classes and the messages they accept—information which is readily available (and more current) in the online references that are included with Xcode.
Be sure to get acquainted with those online references in Xcode. There you’ll find detailed listings of all of the Cocoa classes and the messages they support. There’s also an enormous amount of information and sample code on Apple’s Developer Connection web site at developer.apple.com/cocoa.
Did you find an error? Is something missing? Post your comment or suggestion below!
Comments (6) post
I’ve heard of Cocoa and Objective C before but have never come across a clear, high-level explanation of either. Until now. :)
I gotta say I’m excited to hear that Objective C is dynamic, and inspired by SmallTalk no less! It’s a great time to be a progr
Sorry about the truncated comment! I was using the wrong datatype in my comments database. It’s fixed now.
I’m a long time Objective-C (& perl) user who has just discovered Ruby.
Ruby looks extremely promising.
I like to read documentation like this while I’m offline, mainly during train travel. Is there a PDF-version of this book online?
Hi Gerben, no, right now there’s just the website. I’ll post if a PDF version becomes available.
Besides being able to access C+++ libraries, the nice thing about Objective-C++ is that C+++ excels at automatic-storage objects while Objective-C excels at dynamic-storage objects.
(You can read “automatic-storage” as “stack & “dynamic-storage” as “heap”, except that an automatic-storage object may actually be nested within a dynamic-storage object &, thus, actually on the heap instead of on the stack. Also consider static storage as the same as automatic in this case.)
e.g. A complex number class might be better implemented as a C++ class, while a GUI window might be better implemented as a Objective-C class.
Not sure if this is an error with whatever blog app you’re using… but when I added this feed to my reader, I definitely got three versions of this post on initial download, each with minor edits (regarding the bullshit of Why The Lucky Stiff’s guide, and a minor “it’s-its” swap).
Just though I’d let you know.