[MacRuby-devel] How to properly wrap a C/C++ library for MacRuby (esp.: new/alloc/initialize)

Julian Raschke julian at raschke.de
Sun Oct 18 03:16:19 PDT 2009


Background: I want to let some existing Ruby projects run on MacRuby  
which depend on several C extensions. Since I have done some Objective  
C(++) before, I thought I would just follow the macruby.org guide on  
how to write an Objective C bundle for use with MacRuby. (Seemed  
conceptually easier to me than using FFI.)

My problem is that I don't know how to best conceal the fact that the  
wrapped objects are now NSObjects, not typical C extension objects. I  
can monkey-patch the method signature style, but I feel that it isn't  
so trivial for constructors. The reasons for that probably reach  
deeper into MR/Cocoa than I have looked so far.

The existing Ruby code expects to be able to do:

MyCObject.new(2, 4)

however my wrapped-through-Objective-C library only offers:

MyCObject.alloc.initWithA 2, b: 4

I have tried to just override MyCObject.new to call  
alloc.initWithA:b:, but that breaks as soon as the user wants to  
override "initialize". He may even want to call "super" at a defined  
point in there.

I have also tried writing a "initialize" method that calls  
"initWithA:b:", and calling "alloc.initialize(a, b)" from within  
MyCObject.new. I expected that this may allow for overriding  
#initialize. Instead, it leads to a segfault.

Is there a way to completely hide the fact that MyCObject is actually  
not a plain old Ruby class?

Julian



More information about the MacRuby-devel mailing list