On 2 Feb 2006, at 23:53, Tim Burks wrote:
> To illustrate it, I've written a small article that shows how to wrap
> Dave Thomas' jukebox example with rubycocoa. It is online here:
>
> http://www.rubycocoa.com/ruby-extensions-with-rubycocoa
>
> Please take a look sometime and send me any thoughts or suggestions.
This looks cool.
> Memory deallocation is handled the Cocoa way rather than the Ruby =20
> way. So even though we=92ve gotten rid of all references to our =20
> jukebox object, it=92s not deleted until the Objective-C runtime =20
> cleans up. In our example, that happens when we exit irb.
Isn't it just that ruby's garbage collector hasn't run yet, and so =20
the Ruby reference to the ObjC object is still around? Try putting =20
GC.start before exiting and see if that forces the deallocation.
I've just been playing with RubyInline for simple C interfacing. I =20
wanted to register a global hotkey, which requires accessing Carbon =20
event APIs. Here's a nippet of the code:
class Controller < NSObject
def self.shared_instance
@instance ||=3D Controller.alloc.init
end
inline do |builder|
builder.src_ext =3D "m"
builder.prefix %q{
#undef T_DATA
} # required because Ruby and Quicktime both define T_DATA
builder.include "<Carbon/Carbon.h>"
builder.prefix %q{
EventHotKeyRef gMyHotKeyRef;
EventHotKeyID gMyHotKeyID;
static OSStatus MyHotKeyHandler(EventHandlerCallRef =20
nextHandler,EventRef theEvent,void *userData)
{
rb_eval_string("Controller.shared_instance.hotkey_pressed");
return noErr;
}
}
builder.c_singleton %q{
void register_key(int keycode) {
EventTypeSpec eventType;
eventType.eventClass=3DkEventClassKeyboard;
eventType.eventKind=3DkEventHotKeyPressed;
InstallApplicationEventHandler(&MyHotKeyHandler,=20
1,&eventType,NULL,NULL);
gMyHotKeyID.signature=3D'WBCM'; // choose something here
gMyHotKeyID.id=3D1;
RegisterEventHotKey(keycode, cmdKey, gMyHotKeyID, =20
GetApplicationEventTarget(), 0, &gMyHotKeyRef);
}
}
end
def hotkey_pressed
...
end
end
# During initialisation...
Controller.register_key
--- inline.rb 1970-01-01 01:00:00.000000000 +0100
+++ ../inline.rb 2006-02-03 09:14:59.000000000 +0000
@@ -260,6 +260,7 @@
public
+ attr_accessor :src_ext
def initialize(mod)
raise ArgumentError, "Class/Module arg is required" unless =20
Module =3D=3D=3D mod
# new (but not on some 1.8s) -> inline -> real_caller|eval
@@ -276,6 +277,7 @@
@sig =3D {}
@flags =3D []
@libs =3D []
+ @src_ext =3D "c"
end
##
@@ -310,7 +312,7 @@
unless so_exists and File.mtime(rb_file) < File.mtime(so_name)
=09
- src_name =3D "#{Inline.directory}/#{module_name}.c"
+ src_name =3D "#{Inline.directory}/#{module_name}.#{src_ext}"
old_src_name =3D "#{src_name}.old"
should_compare =3D File.write_with_backup(src_name) do |io|
io.puts
|