MacRuby is a unique blend of Ruby 1.9 and Objective-C. The goal of the MacRuby project is to be 100% compatible syntactically and behaviorally with Ruby 1.9. Because of the project’s goals of seamless integration with Objective-C, however, there are significant differences in the internals of the runtime. The following presents an overview of what is unique to MacRuby vs. the standard Ruby 1.9 ‘MRI’ interpreter.
Class and Object Model
Ruby classes in MacRuby are in fact Objective-C classes. Because it is not yet possible to completely express the Ruby semantics with the Objective-C runtime, some extra bits are still being allocated per class. For example, Objective-C doesn’t allow instance variables to be added at runtime to a class, or classes to be nested, so MacRuby has to work around that.
Every method defined on a class from Ruby is registered with the Objective-C runtime. On a similar note, all Objective-C methods are lazily available from Ruby too. The [Libffi] library is used to Create closures at runtime and inject them, either in the Objective-C runtime or YARV, but also to perform C or Objective-C calls.
All Ruby classes inherit from NSObject, the root class of most Objective-C objects in the Cocoa world. This means that by default, all Ruby objects respond to a bunch of handful methods defined in NSObject, including some that are required by the GC.
Objective-C classes are imported in the Ruby runtime on demand, along with their full hierarchy.
All Ruby objects are actually Objective-C objects. The basic Ruby object structure was modified to conform to the basic Objective-C object structure. This means that Ruby objects can be passed to the Objective-C runtime without any conversion. They will be recognized in the Objective-C world because they have an Objective-C class.
Vice-versa, Objective-C objects can be passed to the Ruby VM without any conversion. They will be recognized as pure Objective-C objects.