Ruby Meets Cocoa

16
Ruby meets Cocoa Eloy Duran

description

Eloy Duran's slide's of his presentation at the RubyenRails 2007 conf in Amsterdam.

Transcript of Ruby Meets Cocoa

Page 1: Ruby Meets Cocoa

Ruby meets CocoaEloy Duran

Page 3: Ruby Meets Cocoa

class EloyDuran < HomoSapiensObject

attr_reader http://superalloy.nl/blog attr_writer [email protected]

def self.rubycocoa_os_stuff { :qtkit => 'Back in the day, when it was impossible to access C functions provided with a framework from the Ruby side of the RubyCocoa bridge. I had written a wrapper for QTKit which implemented all those functions.', :bridge_doc => 'A tool that ships with RubyCocoa which parses the Apple Objective-C HTML reference files and transforms the Objective-C methods to the way that calling is done from Ruby. The outputted ruby source files are then passed to rdoc which creates HTML and ri documentation.', :ActiveRecordBindingsSupport => 'A set of Cocoa subclasses, included with RubyCocoa, which try to be the glue between Cocoa Bindings and ActiveRecord. Allowing you to create a CoreData like application, but instead using ActiveRecord as a backend.' } end def self.recent_super_secret_commercial_project "Can't say much about it, but basically it's a platform consisting of a server and clients, which handle the distribution of media files used in narrowcasting. The server is a Rails application with a Flex frontend to add media files to playlists. The clients are mac mini's that communicate with the server to see what they need to display and then start the transfer via the bittorrent protocol. The clients also run a application which displays the media files when needed. This player is created with RubyCocoa." endend

Who is this guy?!

Page 4: Ruby Meets Cocoa

What is this Cocoa thing?

• Cocoa is Apple’s native object-orientated programming environment for Mac OS X, written in Objective-C.

• It’s derived from the NeXTSTEP and OPENSTEP programming environments developed by NeXT in the late 80’s. This is still visible in the prefix of all the class names, which is the acronym “NS” (for NeXTSTEP): NSString, NSArray etc.

• It is a collection of frameworks, most notably:

• Foundation Kit / Foundation: not tied to the GUI eg: string manipulations.

• Application Kit / AppKit: to interact with the GUI eg: buttons, text fields.

Page 5: Ruby Meets Cocoa

Objective-C

• Created by Brad Cox and Tom Love in the early 80’s, influenced heavily by Smalltalk.

• Object-oriented programming language.

• A strict superset of C, which means that any C code can be compiled by an Objective-C compiler.

• Most of the syntax is inherited from C, the object-orientated syntax was added to allow Smalltalk-style messaging.

• Like C, lots of semi-colons....

Page 6: Ruby Meets Cocoa

Objective-C example

// An instance method called sayMessage:toPerson:

- (void)sayMessage:(NSString)msg toPerson:(NSString)person

{

NSLog(@”%@ %@!”, msg, person);

}

Let’s say we have an instance of a class,which implements the following instance method:

When we pass the instance the sayMessage:toPerson message,a line will be printed to the console by NSLog()

[theInstance sayMessage:@”Hello” toPerson:@”World”]; //=> “Hello World!”

Page 7: Ruby Meets Cocoa

The most important thing about Objective-C to me....

Page 8: Ruby Meets Cocoa

I’m a total (Obj-)C noob!by choice...

Page 9: Ruby Meets Cocoa

So, I need another solution to make use of Cocoa’s

excellent facilities.

Page 10: Ruby Meets Cocoa

RubyCocoa

Page 11: Ruby Meets Cocoa

RubyCocoa History• Late 2001: Started by Hisakuni Fujimoto. Release 0.1 - 0.4

• 2002 - 2006: Release 0.5. More committers joined: Chris Thomas, Kimura Wataru, Jonathan Paisley, Tim Burks.

• 2006-05: Laurent Sansonetti (Apple inc) joins the committers list in order to make RubyCocoa a first class citizen among the programming languages supported in Mac OS X10.5 (leopard). Most notable is the addition of Libffi/BridgeSupport.

• 2006-11: I joined the committers list to add BridgeDoc.

• 2006-12: First preview (0.9) of the Apple branch was released.

• 2007-01: Second preview (0.10) was released.

• 2007-05: Third preview (0.11) and now officially the current stable release was released.

Page 12: Ruby Meets Cocoa

What is RubyCocoa?• It is a real bridge between the Ruby & Objective-C programming

languages. Which means it isn’t limited to a predefined set of Cocoa frameworks.

• It works by creating a Ruby proxy class for a Objective-C class. The method calls are sent through the use of Libffi. Which also allows us to call C functions even though they aren’t methods on objects.

• The classes are imported lazily. Meaning that RubyCocoa imports them only on demand.

• BridgeSupport allows RubyCocoa to bridge other parts of a framework, like C structures, constants.

• Because it’s a real bridge, it works both ways. Thus you can mix Ruby and Objective-C inside your app.

Page 13: Ruby Meets Cocoa

RubyCocoa example

// An instance method called sayMessage:toPerson:

- (void)sayMessage:(NSString)msg toPerson:(NSString)person

Let’s say we have an instance of a Objective-C class,which implements the following instance method:

Ruby doesn’t have named parameters (yet?), so the selectors have to be translated in a way that we can use it:

# Traditional syntaxtheInstance.sayMessage_toPerson_('Hello', 'World')# Lazy traditional syntaxtheInstance.sayMessage_toPerson('Hello', 'World')# Alternative syntaxtheInstance.objc_send(:sayMessage, 'Hello', :toPerson, 'World')

Page 14: Ruby Meets Cocoa

Time for some action!

Let’s all pray uncle Murphy won’t act upon his law.....Or laugh, whichever you prefer.

The state of the art MailDemo sample

Page 15: Ruby Meets Cocoa

Questions?

Page 16: Ruby Meets Cocoa

Readfood:•Hisakuni Fujimoto: http://fobj.com/hisa/d•Laurent Sansonetti: http://chopine.be/lrz/diary•Me: http://superalloy.nl/blog

•RubyCocoa website/wiki: http://rubycocoa.sf.net•Drop by in the irc channel (freenode): #ruby-osx•MailingLists: http://rubycocoa.sf.net/GettingSupport

•RubyCocoa tutorials: http://rubycocoa.com•Original MailDemo tutorial: http://cocoadevcentral.com/articles/000080.php

•Unlimited coding inspiration: http://www.johnnyweb.fr