The ObjCCompiler module compiles your Obj-C classes into bundles and requires them so you can use them from Ruby.
There are a number of ways to load classes defined from Cocoa into Ruby. The solution presented here works best if the classes written in Obj-C are also meant to be used from Ruby. The solution requires RubyCocoa and assumes that you've written your application using Rucola.
Let's assume you're working on a project that implements a Match class that wraps SearchKit matches. And you have a match.m somewhere in your lib directory.
Your implementation needs one extra line, this function is invoked by the Ruby interpreter when you try to load a bundle.
void Init_Match() {}
After that you need to
- Compile your code to a bundle
- require the code
- Import the class into the namespace
We've wrapped these three steps into one method.
ObjCCompiler.require('lib/search_kit/match', 'WebKit')
Sometimes you might want to write a part of the class in Ruby, note that your Obj-C class ended up in the OSX namespace, so you might need to assign it to a new constant.
module SearchKit
Match = OSX::Match
class Match < OSX::NSObject
def inspect
"#<SearchKit::Match:#{object_id} score=#{score}>"
end
end
end
Copyright © 2009 Fingertips, Eloy Duran <eloy@fngtps.com>, Manfred Stienstra <manfred@fngtps.com>