RubyCocoa
Encyclopedia
RubyCocoa is a Mac OS X
Mac OS X
Mac OS X is a series of Unix-based operating systems and graphical user interfaces developed, marketed, and sold by Apple Inc. Since 2002, has been included with all new Macintosh computer systems...

 framework
Software framework
In computer programming, a software framework is an abstraction in which software providing generic functionality can be selectively changed by user code, thus providing application specific software...

 that provides a bridge between the Ruby
Ruby (programming language)
Ruby is a dynamic, reflective, general-purpose object-oriented programming language that combines syntax inspired by Perl with Smalltalk-like features. Ruby originated in Japan during the mid-1990s and was first developed and designed by Yukihiro "Matz" Matsumoto...

 and the Objective-C
Objective-C
Objective-C is a reflective, object-oriented programming language that adds Smalltalk-style messaging to the C programming language.Today, it is used primarily on Apple's Mac OS X and iOS: two environments derived from the OpenStep standard, though not compliant with it...

 programming languages, allowing the user to manipulate Objective-C object
Object (computer science)
In computer science, an object is any entity that can be manipulated by the commands of a programming language, such as a value, variable, function, or data structure...

s from Ruby, and vice-versa. It makes it possible to write a Cocoa
Cocoa (API)
Cocoa is Apple's native object-oriented application programming interface for the Mac OS X operating system and—along with the Cocoa Touch extension for gesture recognition and animation—for applications for the iOS operating system, used on Apple devices such as the iPhone, the iPod Touch, and...

 application completely in Ruby as well as to write an application that mixes Ruby and Objective-C code. As of 2008, an Apple project called MacRuby
MacRuby
MacRuby is an implementation of the Ruby language that runs on the Objective-C runtime and CoreFoundation framework under development by Apple Inc. which "is supposed to replace RubyCocoa". It is based on Ruby 1.9 and uses the high performance Low Level Virtual Machine compiler infrastructure...

 is under development to replace RubyCocoa.

Some useful applications of RubyCocoa are exploration of a Cocoa object's features with irb
Interactive Ruby Shell
Interactive Ruby Shell is a shell for programming in the object-oriented scripting language Ruby. The program is launched from a command line and allows the execution of Ruby commands with immediate response, experimenting in real-time...

 interactively, prototyping of a Cocoa application, writing a Cocoa application that combines the features of Ruby and Objective-C and wrapping Mac OS X's native GUI
Gui
Gui or guee is a generic term to refer to grilled dishes in Korean cuisine. These most commonly have meat or fish as their primary ingredient, but may in some cases also comprise grilled vegetables or other vegetarian ingredients. The term derives from the verb, "gupda" in Korean, which literally...

 for a Ruby script.

RubyCocoa is free software
Free software
Free software, software libre or libre software is software that can be used, studied, and modified without restriction, and which can be copied and redistributed in modified or unmodified form either without restriction, or with restrictions that only ensure that further recipients can also do...

, released under both the Ruby License
Ruby License
The Ruby License is the open-source license applied to the Ruby programming language and also available to be used in other projects.The Free Software Foundation comments:...

 and the LGPL.

History

RubyCocoa was started in 2001 by Hisakuni Fujimoto when he implemented a Ruby extension module to wrap NSObject and NSClassFromString function. Later it was integrated with Project Builder (which later became Xcode
Xcode
Xcode is a suite of tools, developed by Apple, for developing software for Mac OS X and iOS. Xcode 4.2, the latest major version, is available on the Mac App Store for free for Mac OS X 10.7 , and on the Apple Developer Connection website for free to registered developers Xcode is a suite of tools,...

).
In 2002 the project was registered on SourceForge and the development team began to grow.

In 2006 the committers list was first joined by a developer from Apple, Laurent Sansonetti, and then a RubyCocoa presentation was made during WWDC. Apple stated that RubyCocoa will be included and supported in Mac OS X v10.5
Mac OS X v10.5
Mac OS X Leopard is the sixth major release of Mac OS X, Apple's desktop and server operating system for Macintosh computers. Leopard was released on 26 October 2007 as the successor of Tiger , and is available in two variants: a desktop version suitable for personal computers, and a...

 “Leopard”.

In August 2008, Sansonetti confirmed that MacRuby
MacRuby
MacRuby is an implementation of the Ruby language that runs on the Objective-C runtime and CoreFoundation framework under development by Apple Inc. which "is supposed to replace RubyCocoa". It is based on Ruby 1.9 and uses the high performance Low Level Virtual Machine compiler infrastructure...

 "is supposed to replace RubyCocoa." in the future.

How Does the Bridge Work?

RubyCocoa is sometimes interpreted as a set of bindings to the Cocoa frameworks, which is false. RubyCocoa is a real bridge between the Objective-C and Ruby programming languages.

Lazy Class Import

RubyCocoa will import the Objective-C classes into the Ruby world on demand. For example, when you access OSX::NSTableView for the very first time in your code, RubyCocoa will retrieve all the necessary information regarding this class from the Objective-C runtime and create a Ruby class of the same name that will act as a proxy. It will also import in the same way all the inherited classes.

Forwarding Messages

As stated earlier, RubyCocoa creates special proxy objects. Every time you send a Ruby message to a proxy object, RubyCocoa will try to forward it to the embedded Objective-C instance, by translating the message name to an Objective-C selector and asking the Objective-C runtime to forward it.

If an exception is raised from the Objective-C world, RubyCocoa will convert it to a Ruby exception and forward it to you.

RubyCocoa uses the libffi
Libffi
libffi is a foreign function interface library. It provides a C programming language interface for calling natively compiled functions given information about the target function at run time instead of compile time...

 library to call the Objective-C methods implementations.

Automatic Method Overriding

RubyCocoa makes it easy to override an Objective-C method from Ruby, either in a subclass or directly to the class (as you would do in Objective-C using a category).

Once your method is inserted, RubyCocoa will retrieve the signature of the existing Objective-C method and inject a new one to the Objective-C runtime, of the same signature, but which now points to your code.

To accomplish this, RubyCocoa uses the libffi
Libffi
libffi is a foreign function interface library. It provides a C programming language interface for calling natively compiled functions given information about the target function at run time instead of compile time...

 library to dynamically create a closure that will call the Ruby method, and just passes a pointer to that new closure to the Objective-C runtime.

Accessing the C Bits

Due to the nature of the Objective-C language, you can freely use C
C (programming language)
C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system....

 from Objective-C code. In order to bridge the relevant C parts of an Objective-C framework, such as C structures, functions, enumerations, constants and more, RubyCocoa relies on the BridgeSupport project.

RubyCocoa will interpret at runtime the BridgeSupport files (using the very fast libXML
LibXML
libxml2 is a software library for parsing XML documents. It is also the basis for the libxslt library which processes XSLT-1.0 stylesheets.-Description:...

2's xmlTextReader) and accordingly handle their content. It will for instance construct the Ruby proxy classes for the C structures and also create the functions.

Note that the costly operations, such as localizing the symbols, are done on demand, and obviously only once.

Format Strings

RubyCocoa is able to detect APIs that use format strings, like NSLog or NSString.stringWithFormat, and appropriately convert the variable arguments to the types specified in the format string.

Function Pointers

RubyCocoa allows you to pass Ruby Proc objects as function pointer arguments. It will then use the libffi
Libffi
libffi is a foreign function interface library. It provides a C programming language interface for calling natively compiled functions given information about the target function at run time instead of compile time...

 library to dynamically create a closure and pass it to the underlying function/method.

How to start a Cocoa application written in Ruby

When you install RubyCocoa, the corresponding Xcode
Xcode
Xcode is a suite of tools, developed by Apple, for developing software for Mac OS X and iOS. Xcode 4.2, the latest major version, is available on the Mac App Store for free for Mac OS X 10.7 , and on the Apple Developer Connection website for free to registered developers Xcode is a suite of tools,...

 templates are installed
automatically. So when you start a new project, select Cocoa-Ruby Application project type and all necessary files will be generated.

How to call Objective-C methods from Ruby

To invoke an Objective-C method, you replace each colon in the method name except the last with an underscore. Thus, for example, the NSWindow instance method initWithContentRect:styleMask:backing:defer: becomes initWithContentRect_styleMask_backing_defer.

All Cocoa classes and functions belong to OSX module, so for example, the Objective-C code:


SWindow alloc] initWithContentRect:frame
styleMask:NSTitledWindowMask
backing:NSBackingStoreBuffered
defer:NO]


will become:


OSX::NSWindow.alloc.initWithContentRect_styleMask_backing_defer(frame,
OSX::NSTitledWindowMask,
OSX::NSBackingStoreBuffered,
false)


As you can see, this decreases the code readability by rendering Objective-C parameter naming useless. So, there is another convenient way to write the method calls — the objc_send method, which accepts Ruby symbols as parameter names. For example, the previous code can also be written as:


OSX::NSWindow.alloc.objc_send(:initWithContentRect, frame,
:styleMask, OSX::NSTitledWindowMask,
:backing, OSX::NSBackingStoreBuffered,
:defer, false)

Advantages of RubyCocoa

  • As Ruby is an interpreted language, there's no need to recompile the application frequently during development.
  • Some of Ruby's rich features, like built-in regular expression
    Regular expression
    In computing, a regular expression provides a concise and flexible means for "matching" strings of text, such as particular characters, words, or patterns of characters. Abbreviations for "regular expression" include "regex" and "regexp"...

     support, make writing the code faster, and also make RubyCocoa an ideal tool for prototyping.

Disadvantages

  • Ignoring the speed of the Ruby interpreter, RubyCocoa applications will always be slower than Objective-C/Cocoa applications due to the added overhead of object conversion.
  • Because Ruby isn't thread-safe, it isn't possible to dispatch multiple native threads to execute RubyCocoa code. Ruby's emulated threads, however, can be used.


Category:Mac OS X software
Category:Ruby programming language
The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK