On Sep 27, 2009, at 11:18 AM, Rajinder Yadav wrote:

> Joel VanderWerf wrote:
>> Rajinder Yadav wrote:
>>> Hi all,
>>>
>>> I am wondering if there is a tutorial that will show me how to  
>>> call C code from Ruby?
>> One starting point is the README.EXT file in the ruby source.  
>> There's also a chapter in the pickaxe book (Programming Ruby). As  
>> you get farther along you may want to look into swig, if you are  
>> trying to access an existing library.
> Thanks Joel,
>
> I also came across di and ffi on my search as well and was wondering  
> what is the most efficient technique? or what solution is the most  
> cleanest way to make calls to C/C++ code from Ruby?
>
> If anyone is calling their C/C++ code from Ruby, I would like to  
> hear from them what limitations they may was discovered on the the  
> various methods.

If you write your extension in C, it won't work with JRuby.  I find  
FFI / dl to be infuriatingly difficult to debug at times.  Also when  
distributing a DL/FFI gem, you need to teach your users how to set  
LD_LIBRARY_PATH.  Your extension performance will be faster if you  
write in C.  With FFI/DL, it takes more time to call C functions.

I can go on, but my general rule of thumb is if you're wrapping only a  
handful of functions, use FFI.  If you're wrapping a complex library,  
use C.

---
Aaron Patterson
http://tenderlovemaking.com