5. RubyCocoa Examples

When RubyCocoa is installed, several example programs are installed in /Developer/Examples/RubyCocoa.

Several of the RubyCocoa examples are contained in single files.
To run each one, type “ruby file-name” in the Terminal.

example description
speak.rb use AppleScript to speak the contents of a file
fontnames.rb print a list of the fonts installed on your system
itunes_albums.rb use AppleScript to get a list of albums from iTunes
sndplay.rb play each of the installed system sounds sequentially in a single thread
sndplay2.rb play each of the installed system sounds in a separate thread
sndplay3.rb the same as sndplay2.rb but using alternate message passing syntax
HelloWorld.rb create a window and a few buttons without a nib file
TransparentHello.rb create a transparent window without a nib file

Some of these examples show that interface elements can be created without the use of nib files, but they are far from complete working applications. I’ve searched for other examples and documentation of nib-less Cocoa programming; the best information I’ve found is that “it’s possible” but “don’t try it!” In the future I hope to have more detail than that, but for now I have to stay with nib files for serious Cocoa work.

Several RubyCocoa examples are built using Makefiles in the associated directories. In general, they are much fuller applications than the single-file examples.

example description
dotview a RubyCocoa rewrite of Apple’s popular DotView sample program.
On systems with Apple’s developer tools installed, DotView is in /Developer/Examples/AppKit/DotView. Readers will find that the Ruby version is incredibly concise and understandable, although as always, it’s easier to read than to write in a new dialect.
mk_app0 a very simple application that writes the current time in a window when a button is clicked.
mk_app1 an extension of mk_app0 that adds a view to the window and some buttons that select the color to be drawn in the view.
multinib an example that illustrates the loading of multiple nib files by their associated window controllers. Two different ways of writing window controllers are shown.
thread0 an application that creates a separate Ruby thread that updates a message in a window as it runs.
Hakoiri-Musume a puzzle game.
MyViewer a complex example that illustrates notifications, image views, and more nib-less programming.

These examples are all built using Xcode. Many of them have outdated project files (with the .pbproj extension), but Xcode will update them automatically when they are opened.

example description
simpleapp a simple but beautiful example illustrating transparency, image views, and threads.
pb_app0 a minimal application consisting of a single window and a few actions.
HybridLangApp a small example showing that Objective C and Ruby objects can be used equivalently.
Pong a small arcade game that uses a thread to control the game state.
CurrencyConverter a key value coding example from Apple rewritten in Ruby. The original Apple example is online here.
RubyRaiseMan a Ruby implementation of the RaiseMan example from Aaron Hillegass’ Cocoa Programming for Mac OS X.
RubyTypingTutor a Ruby implementation of the TypingTutor example from Aaron Hillegass’ Cocoa Programming for Mac OS X.
RubySpotlight a new example illustrating RubyCocoa interaction with Spotlight.

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

Comments (0) post