Calling 'C' code from Ruby

Hi all,

I am wondering if there is a tutorial that will show me how to call C
code from
Ruby?

I am planning on playing with the idea of adding encryption support for
Ruby
that will be faster than current (pure) native Ruby code plug-in out
there
called crypt. I may start with GnuPG to offer something to the
community.

I am new to Ruby and if I can get this done, it will be my first
submission to
the Open Source community.


Kind Regards,
Rajinder Y.

http://DevMentor.org
Do Good ~ Share Freely

Rajinder Y. 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.

Joel VanderWerf wrote:

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.


Kind Regards,
Rajinder Y.

http://DevMentor.org
Do Good ~ Share Freely

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

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 P.
http://tenderlovemaking.com

Aaron P. wrote:

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.

Hi Aaron. thank you for the feedback. This is really good to know,
definitely
helpful in terms of the thing I want to avoid. Hopeful it’s not to
difficult to
get a “hello world” first step going =P

I guess I also want to evaluate/determine which technique is really
efficient
with passing large pools of data between C and Ruby.


Aaron P.
http://tenderlovemak

Kind Regards,
Rajinder Y.

http://DevMentor.org
Do Good ~ Share Freely

Dear Rajinder,

there’s also RubyInline
(http://www.zenspider.com/ZSS/Products/RubyInline/)
and Rice (http://rice.rubyforge.org/) for bringing C and Ruby together.
I can’t say whether they’d be better adapted for what you are trying to
do,
but I’d be interested to learn which of the different Ruby/C connection
possibilities gives you a best result.

Best regards,

Axel

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs