Sometimes I think Apple's out to get me!
Wednesday, March 5, 2008 at 2:37AM
Richard Warren

So, I write an article about installing Ruby for Rails on Tiger. Then Apple announces that RoR will be included in Leopard.

Then I write an article about using TimeMachine over a wireless connection. Just days after the magazine hits the stand, Steve Jobs announces Time Capsule.

Now, I've just finished a pair of articles on RubyCocoa. They haven't even been published yet, but Apple's already at it again.

Today, I just learned about a new, open source project backed by Apple, called MacRuby.

MacRuby is a Ruby 1.9 port that runs on top of Objective-C. It's not ready for prime time yet, but it looks promising. First off, it is Ruby 1.9--which I think is a great thing. Among other things, this means it will be much, much faster than RubyCocoa, which uses Ruby 1.8.

From there, things get really interesting--and just a bit odd. All MacRuby objects are subclasses of NSObject. Because of this, they inherit all the base object methods from both Objective-C and Ruby. They've also added an expanded syntax for keyed attributes in method calls.

So,

[person setFirstName:first lastName:last];

becomes

person.setFirstName(first, lastName:last)

or

person.setFirstName first, :lastName => last

You can even write your own classes in Ruby that use keyed attributes:


def setFirstName(first, lastName:last)
@name = "#{first} #{last}"
end

Of course, you can do all the cool RubyCocoa tricks. Make calls back and forth between the Objective-C and Ruby portions of your code. Import Cocoa frameworks. Etc. But, in MacRuby, it all looks just a little bit tighter.

For example, MacRuby's String, Array and Hash classes are simply subclasses of Objective-C's NSString, NSArray and NSDictionary. This lets you transparently pass objects between Ruby and Objective-C.

I look forward to playing around with this project as it develops.

-Rich-

Article originally appeared on Freelance Mad Science Labs (http://www.freelancemadscience.com/).
See website for complete article licensing information.