[MacRuby-devel] Interfacing with C (not objective C)

Martin Hess martinhess at mac.com
Mon Mar 16 20:37:21 PDT 2009


My apologies if this has been covered somewhere else, but how do you  
interface with C in MacRuby. I know you can just write a C extension  
for Ruby but it appears that MacRuby makes this easier but it is  
unclear to me how far its support goes and when you should write an  
extension.

Someone was asking about OpenGL in another thread which is a great  
example of a more difficult case but I don't think it covers all cases  
in a general sense. I'll break up the problem as I see it:

1) External C routines can be asynchronous
	a) Memory handed to the asynchronous routine can't move i.e. no  
garbage collection
	b) Call backs (i.e. passing in function pointers to a C routine)
		i.  Asynchronous routines can show up at anytime

2) Function pointers as parameters to external C routines
	a) Function signature has to match exactly i.e. can't pass in a Ruby  
method pointer

3) Memory
	a) Allocated by external C routine
		i) How to delete if it is the callers responsibility to delete
	b) Allocated by caller
		i) How to allocate and get valid pointer to memory that isn't going  
to get garbage collected away
	c) Reading and writing allocated memory
		i) This is raw memory with no type information
		ii) How do you give it shape so you can read and write symbolically  
i.e. no peek/poke
		iii) Reading and writing packed bitfields


So how are each of these things done in MacRuby? 


More information about the MacRuby-devel mailing list