From: Einar Karttunen Date: 2003-07-16T18:37:21+09:00 Subject: Passing ruby objects to other applications Hello I am wondering what is the most elegant way of passing ruby objects to a non-ruby application as e.g. callbacks. As the only references to the objects may live inside the non-ruby application, some kind of reference counting is needed in order to make it possible for the Ruby GC to know when the objects can be deleted. The first solution that came to my mind was to add a hash table, which would contain a object -> ref_count mapping. When an object would be passed to the C side it would be inserted to the hash / it's reference count would be increased. When the C struct would be destroyed the reference count would be decremented and if it would have reached 0, the key would be removed. This would work because the GC would see the objects and correctly mark them. This also avoids potential problems with the C program using things like realloc. class RefCount < Hash def inc(k) v = self[k.id] if v v[0] += 1 else self[k.id] = [1, k] end end def dec(k) v = self[k.id] if v if v[0] <= 1 delete k.id else v[0] -= 1 end end end end Is this the way to go, or is there a better one? - Einar Karttunen