calling 'C' code from Ruby

R

Rajinder Yadav

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 Yadav

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

Joel VanderWerf

Rajinder said:
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.
 
R

Rajinder Yadav

Joel said:
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.

--
Kind Regards,
Rajinder Yadav

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

Aaron Patterson

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.
 
R

Rajinder Yadav

Aaron said:
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.

--
Kind Regards,
Rajinder Yadav

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

subscribing to ruby-core 2
Rails on Ruby 1.9 2
building ruby from source 13
building from source 2
ruby-pg gem fails to install 10
creating a openstruct from xml 4
Happy Holidays 4
haml 8

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top