From: "Carlo E. Prelz" Date: 2013-02-21T00:41:30+09:00 Subject: Re: Python C API vs Ruby C API Subject: Python C API vs Ruby C API Date: mer 20 feb 13 11:57:22 +0900 Quoting Patrick (patrick@spellingbeewinnars.org): > The Ruby C API looks lighter but much easier to work with. Once you get the hang, it is very straightforward. I do not know Python, but compared with JNI, Ruby's way of interacting with C code is a pleasure to work with. > Is VALUE the only type? Ruby being a pure OO language, everything is an object, and thus only one type needs to travel through the border. But once you have a VALUE, you can potentially access the whole object (i.e. call all of its methods) from the C world. For those classes that are most often used (numbers, strings, arrays and hashes, to name a few), there are helping functions. You need to learn them, but once you learned them, things work well. If you look at Ruby's source code, you will find tons of examples. Under /ext you will find all those libraries that are shipped with Ruby and which have C code. > Is it true that I do not have to manually increment and decrement > values as well? If you mean reference counts, the only cases when some complexity appears is when you pass VALUEs to C and you want to use them after the method call has returned (in background threads, for example). Otherwise, the objects you create from C are correctly garbage-collected. When you create you own Ruby classes in C, you assign to them a C structure, which is allocated for each instance at object creation, and which you can access from C-code methods. There you can safely store all C stuff that you will need along the life of the object. You must provide a function where your stuff is freed, which is called when the object is GCed. > I was also wondering about extending Ruby. If I create a C module > FOO and another module POO, can FOO call functions from POO or is > there a way to make them visible? I might like to call TK from > another C module The classes and modules you create from C become full-fledged Ruby classes and modules. You can add C methods to classes you define under Ruby, and vice-versa. I suggest that you download the Ruby source. Inside the directory you will find a file called README.EXT which has much information about this topic. I personally needed a good chunk of time and effort to make sense of that, but it has not been wasted time. Carlo -- * Se la Strada e la sua Virtu' non fossero state messe da parte, * K * Carlo E. Prelz - fluido@fluido.as che bisogno ci sarebbe * di parlare tanto di amore e di rettitudine? (Chuang-Tzu)