O'Reilly logo

MacRuby: The Definitive Guide by Matt Aimonetti

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 11. MacRuby in Objective-C Projects

MacRuby is great, but you might already have a Cocoa application written in Objective-C. Rewriting all of it in MacRuby would probably be fun, but maybe not really wise. You might also want to experiment with MacRuby without fully committing to it yet. Another reason to mix the two languages is if you want to better test your Objective-C code, but are not pleased by the existing Objective-C tools. Whatever reason you have, MacRuby is really easy to use within your existing app.

API

MacRuby is a framework, so to use it in your Objective-C project, you just need to add it to your project and import the headers:

#import <MacRuby/MacRuby.h>

Once you have added the framework and imported the headers, you can call out to files or snippets of MacRuby from Objective-C. Let’s take a minute to look at the API provided by the MacRuby framework:

#import <Foundation/Foundation.h>

@interface MacRuby : NSObject

/* Get a singleton reference to the MacRuby runtime, initializing it before if
 * needed. The same instance is re-used after.
 */
+ (MacRuby *)sharedRuntime;

/* Evaluate a Ruby expression in the given file and return a reference to the
 * result.
 */
- (id)evaluateFileAtPath:(NSString *)path;

/* Evaluate a Ruby expression in the given URL and return a reference to the
 * result. Currently only file:// URLs are supported.
 */
- (id)evaluateFileAtURL:(NSURL *)URL;

/* Evaluate a Ruby expression in the given string and return a reference to the
 * result. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required