RubyConf: Mac OS X and Ruby

Posted by Nick Sieger Sat, 21 Oct 2006 18:58:33 GMT

Laurent Sansonetti, who works at Apple Computer, is now in charge of maintaining Ruby inside the OS.

History of Ruby in the OS

  • 10.2 shipped with Ruby 1.6.7
  • 10.3 shipped with Ruby 1.6.8
  • 10.4 shipped with 1.8.4, and the latest software update has Ruby fixes to allow compilation of C extensions

Apple is actively looking to improve Ruby support in future versions of the OS in several areas:

  • Ruby.framework
  • 64 bit support
  • Controlling scriptable applications
  • Cocoa development

Framework

Why a framework? It’s easier for Mac development. It allows multiple versions, and lets you bundle Ruby inside your application. It’s also compatible with original layout /usr/bin/ruby => /System/Library/Frameworks/Ruby.framework/Current/bin/ruby

Misc

  • Include IRB support, RubyGems, plus important gems: Rake, Rails, libxml2 and sqlite3 bindings, DNSSD (Zeroconf)

Controlling scriptable applications

Traditionally applications are scripted by AppleScript. Apple Events are the IPC glue between the two.

RubyAEOSA is a Ruby wrapper for Apple events, that was started in 2001, but not active since 2003. The code required is unnecesarrily verbose. Instead, you could wrap and execute with AppleScript, but it’s slower and limited by knowledge of AppleScript.

RubyOSA is a new project created by Apple, intended to be a successor to RubyAEOSA, under active development and used today. It has a much more Rubyish API, generating Ruby code on the fly and sending events lazily. Apple events are completely hidden.

require 'rbosa'

itunes = OSA.app('itunes')
itunes.current_track
itunes.current_track.name
itunes.current_track.artist
itunes.current_track.size
itunes.play
itunes.pause
itunes.sound_volume = 0
100.times {|i| itunes.sound_volume = i; sleep 0.1 }

0.1.0 release is coming soon, gem install rubyosa.

Cocoa development

Mac development framework, mostly using Objective-C. RubyCocoa is a Ruby/Objective-C bridge, now under active development with support from Apple. It creates Obj-C classes to Ruby proxies and vice-versa. Messages and exceptions are forwarded, and types are converted.

It uses a message convention. ‘:’ are substituted with ‘_’ in the method name (selector). The final ‘_’ can be omitted, but can lead to ambiguities. It may be mandatory in a future release.

Some challenges remain in the Ruby/Obj-C bridge.

Problem: Speed of method invocation. Solution: libffi from gcc, allows invocation of arbitrary foreign function interfaces.

Problem: Maintenance. Currently RubyCocoa needs to be rebuilt to take into consideration new APIs, so to solve, a metadata framework is being built to allow the information to be more dynamic, and a metadata generator tool will help with creating a metadata file for new APIs

These latest developments are available from the apple-unstable branch in SVN from rubyforge.

Future

  • “Toll free bridge” -- implement Ruby primitive types with CoreFoundation objects so that no type conversion is necessary
  • Garbage collection -- Objective-C will be getting garbage collection in the future, and the idea is to implement the Obj-C garbage collector in Ruby through Obj-C APIs.

Tags , ,  | 5 comments | no trackbacks

Comments

  1. Avatar cabo said 1 day later:

    Question for the hallway: How do RubyOSA and rb-appscript compare? The latter works well today, but does not support “attachability”, AFAIU. But where do we go from here?

  2. Avatar has said 1 day later:

    Reply to cabo:

    I’m not doing any comparisons of rb-appscript and RubyOSA till RubyOSA 0.1 is out and I’ve had a chance to look at it. But to answer to the rest of your query:

    RubyOSA and rb-appscript are both just bridges from the existing CRuby interpreter to OS X’s Apple Event Manager. For attachability you need an OSA language - essentially, a Ruby interpreter wrapped up as a Component Manager component. (Same idea as ActiveScriptRuby, except ASR’s based on Windows COM, of course.)

    You can get already a basic OSA component for Ruby, also called RubyOSA (no relation), from:

    http://homepage.mac.com/philip_aker/osa/osa.html

    This supports the core OSA operations - loading and storing OSA script files (.scpt), compiling from source, and executing - and should be adequate for simple tasks such as attaching alarm scripts to iCal. However, it doesn’t support the more advanced OSA features such as sending and receiving events -needed for things like Folder Actions and Mail rules - so its usefulness is somewhat limited.

    FWIW, I wouldn’t mind writing a full Ruby OSA component myself, but I’d need some help from a Ruby expert first as I don’t know anything about CRuby embedding. (Offers welcome.)

    HTH

  3. Avatar cabo said 2 days later:

    Thanks, has -- no wonder I got confused if there are two things that are called RubyOSA. Yes, your comment did help :-)

  4. Avatar Anonymous Coward said 20 days later:

    Where can I find more information on Ruby.framework?

  5. Avatar Nick said 20 days later:

    I don’t know of any published information about this. You might wish to contact Laurent to see what the timeframe is for this (I suspect we’d see something in Leopard or one of the updates to it).

Trackbacks

Use the following link to trackback from your own site:
http://blog.nicksieger.com/articles/trackback/88