Wondeful joy! Mix Ruby and Objective-C with MacRuby
How great is this. You can use any language you want in your MacRuby projects. Want to add in some code you already wrote in Objective-C++? No problemo!
Check out the cool screenshot below which shows everything in action! (You might have to click on the full post view to get the picture).
Also, you might be wondering how to get full bi-directional bridge going on between Ruby and Objective-C/Cocoa. In otherwords, how do you call/access/include/import/require your own Objective-C classes from Ruby and your Ruby objects from Objective-C? It’s actually done sort of automagically. If you have a Ruby class Foo and a ObjC class Bar…
(You seem to have to use NSClassFromString(@”FooController”) to avoid getting a linker error. Apparently the bridge is not yet running at compile/link time. Also, you (seem to) have to type your bridged objects to id in ObjC, although maybe there’s a way around both those limitations that I don’t know about yet.)
# Foo.rb
class Foo
def foo
puts 'FOO'
bar = Bar.new
puts bar.bar
end
def baz
return "BAZ"
end
end
// Bar.h
#import <Cocoa/Cocoa.h>
@interface Bar : NSObject {
}
- (NSString*)bar;
@end
// Bar.m
#import "Bar.h"
@implementation Bar
- (NSString*)bar; {
id foo = [[NSClassFromString(@"FooController") alloc] init];
NSLog(@"%@", [foo baz]);
return @"BAR";
}
@end
And you will get this output when you call Foo.foo:
FOO
2009-02-10 02:46:28.017 Untitled[37257:10b] BAZ
BAR
Boing boing boing! Enjoy!!!!