|
From: Jonathan P. <jp...@dc...> - 2006-01-31 14:02:12
|
On 31 Jan 2006, at 6:46, Tim Burks wrote:
>> game_controller = OSX::NSDocumentController.sharedDocumentController
>> game = game_controller.documents.objectAtIndex(0)
>
> From there, I could directly call the methods of my object from Ruby:
>> game.start
>> game.sendEvent_forPlayer(10, 0)
>> game.stop
The same should work in the other direction too: assuming you can get
a reference in objc to one of your ruby objects, you should be able
to call methods on it. e.g.:
In ruby:
game.setRubyObject(someobj)
Then in objc:
[rubyObject someMethod: anArgument];
.. assuming you've got an instance variable called 'rubyObject' of
type 'id' that has a setter method like:
-(void)setRubyObject:(id)obj
{
[obj retain];
[rubyObject autorelease];
rubyObject = obj;
}
Another, possibly simpler, solution is to define an ObjC class in Ruby:
class Foo < OSX::NSObject
# ...
def myMethod(arg1)
puts arg1
end
end
Then, in objc:
Class fooClass = NSClassFromString(@"Foo");
id fooInstance = [[fooClass alloc] init];
[fooInstance myMethod: @"hello"];
[the above code is untested - I think the idea is right though]
I imagine there'll be various compiler warnings since there are no
prototypes for the messages being sent to the 'id' variable type. An
objc interface declaration should fix that.
Cheers,
Jonathan
|