Ruby Xocolatl

MacRuby

Some days ago I found the MacRuby project and it seems it will bring a lot of new things to ruby+cocoa programming. And as I've been reading and writing some RubyCocoa apps in the last months I want to share the joy it gave me.

First of all, it will use the new YARV interpreter that was released with the 1.9 version of ruby last Christmas which is faster than the so called 2008 text: MRV2026 correction: MRI2026Typo, it was always MRI, Matz's Ruby Interpreter. Since 1.9, YARV is the VM inside MRI, so the two names I was comparing became the same thing.(the "M" is for Matz), also, it doesn't use the ruby garbage collector, but the cocoa one.2026That was the Objective-C 2.0 garbage collector. Apple deprecated it in OS X 10.8 in favour of ARC (automatic reference counting), and I think that was one of the reasons MacRuby stalled, since it depended on it. This allows the programs to not stop while the garbage collector is running cause the cocoa garbage collector runs in another thread.

Speaking of threads, another feature of the new ruby 1.9 is that it allows users to use POSIX threads (cool!).2026Real threads, yes, but the global interpreter lock still let only one run Ruby code at a time. Actual parallel Ruby came much later, with Ractors in Ruby 3.0 (2020).

Another cool feature is named parameters, which are also useful when calling methods. The message style when calling an object's "method" of Objective-C looks like:

NSAlert *alert = [NSAlert alertWithMessageText: @"What?"
                          defaultButton: @"OK"
                          alternateButton: @"Cancel"
                          otherButton: nil
                          informativeTextWithFormat: @"I am an alert!"];

And the MacRuby counterpart:

alert = NSAlert.alertWithMessageText("What?",
                defaultButton: "OK",
                alternateButton: "Cancel",
                otherButton: nil,
                informativeTextWithFormat: "I am an alert!")

Isn't that sexy?? No more big method names:

alertWithMessageText_defaultButton_alternateButton_otherButton_informativeTextWithFormat

The last beautiful thing I am going to comment about are ruby hashes inheriting from NSDictionary. That's cool because you can pass a ruby hash to methods that need an NSDictionary instead of constructing one in the cocoa way.

The bad things: MacRuby is under development, it needs Leopard and only runs on Intel Macs. But it looks promising.2026It stayed promising until development stopped around 2012. Its lead developer left Apple and built RubyMotion on the same ideas.