Ruby meets Cocoa
          Eloy Duran
Online
http://superalloy.nl/slides/ruby_meets_cocoa.pdf
Who is this guy?!
class EloyDuran < HomoSapiensObject

  attr_reader http://superalloy.nl/blog
  attr_writer e.duran@super...
What is this Cocoa thing?
•   Cocoa is Apple’s native object-orientated programming
    environment for Mac OS X, written ...
Objective-C

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

•   Object-orien...
Objective-C example
                 Let’s say we have an instance of a class,
            which implements the following ...
The most important thing
about Objective-C to me....
I’m a total (Obj-)C noob!
          by choice...
So, I need another solution
 to make use of Cocoa’s
      excellent facilities.
RubyCocoa
RubyCocoa History
•   Late 2001: Started by Hisakuni Fujimoto. Release 0.1 - 0.4

•   2002 - 2006: Release 0.5. More commi...
What is RubyCocoa?
•   It is a real bridge between the Ruby & Objective-C programming
    languages. Which means it isn’t ...
RubyCocoa example
      Let’s say we have an instance of a Objective-C class,
       which implements the following instan...
Time for some action!


     The state of the art
      MailDemo sample

Let’s all pray uncle Murphy won’t act upon his la...
Questions?
Readfood:
•Hisakuni Fujimoto: http://fobj.com/hisa/d
•Laurent Sansonetti: http://chopine.be/lrz/diary
•Me: http://superall...
Upcoming SlideShare
Loading in …5
×

Ruby Meets Cocoa

2,003 views

Published on

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

Published in: Technology
  • Be the first to comment

Ruby Meets Cocoa

  1. 1. Ruby meets Cocoa Eloy Duran
  2. 2. Online http://superalloy.nl/slides/ruby_meets_cocoa.pdf
  3. 3. Who is this guy?! class EloyDuran < HomoSapiensObject attr_reader http://superalloy.nl/blog attr_writer e.duran@superalloy.nl 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 quot;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.quot; end end
  4. 4. 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.
  5. 5. 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....
  6. 6. Objective-C example Let’s say we have an instance of a class, which implements the following instance method: // An instance method called sayMessage:toPerson: - (void)sayMessage:(NSString)msg toPerson:(NSString)person { NSLog(@”%@ %@!”, msg, person); } 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!”
  7. 7. The most important thing about Objective-C to me....
  8. 8. I’m a total (Obj-)C noob! by choice...
  9. 9. So, I need another solution to make use of Cocoa’s excellent facilities.
  10. 10. RubyCocoa
  11. 11. 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.
  12. 12. 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.
  13. 13. RubyCocoa example Let’s say we have an instance of a Objective-C class, which implements the following instance method: // An instance method called sayMessage:toPerson: - (void)sayMessage:(NSString)msg toPerson:(NSString)person Ruby doesn’t have named parameters (yet?), so the selectors have to be translated in a way that we can use it: # Traditional syntax theInstance.sayMessage_toPerson_('Hello', 'World') # Lazy traditional syntax theInstance.sayMessage_toPerson('Hello', 'World') # Alternative syntax theInstance.objc_send(:sayMessage, 'Hello', :toPerson, 'World')
  14. 14. Time for some action! The state of the art MailDemo sample Let’s all pray uncle Murphy won’t act upon his law..... Or laugh, whichever you prefer.
  15. 15. Questions?
  16. 16. 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

×