BT

Ruby FFI Brings Native Library Access to JRuby, MRI

by Werner Schuster on Dec 10, 2008 |

MRI and Rubinius have had support for Ruby native extensions, which allow native code to interact with Ruby code and the Ruby interpreter or VM. Ruby 1.8.0 introduced Ruby/DL which allows to load shared libraries and call the provided functions from Ruby code.  The Ruby/DL documentation shows how to import functions, how to work with and map Ruby to C types, etc. Ruby/DL also provides two way communication with callbacks, ie. it's possible to hand a Ruby block to a C function that expects a function pointer.

Rubinius introduced its own Foreign Function Interface (FFI) which resembles Ruby/DL, although FFI's API looks a bit different, eg. it uses Ruby symbols and array literals for type signatures instead of strings of C code or a custom type specifier formats as Ruby/DL does.

Earlier this year jruby-ffi, built on JNA, was introduced (JRuby doesn't have support for Ruby/DL).

While support on Rubinius and on JRuby was available, this still meant a large part of the Ruby world using MRI was using Ruby/DL or other ways to use shared libraries. To fix this, the ruby-ffi project was created by Wayne Meissner (who already created jruby-ffi) and now provides the same FFI interface as on Rubinius for JRuby, Ruby 1.8.x and Ruby 1.9.x. With this, it's possible to use the same Ruby code across all these Ruby implementations for accessing shared libraries.

Ruby FFI 0.2.0 is now available, installation is possible with gem install ffi. Documentation is available in the project's wiki, a good place to start is the Examples page. Charles Nutter provides some more explanation and samples of using FFI.

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread
Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Discuss

Educational Content

General Feedback
Bugs
Advertising
Editorial
InfoQ.com and all content copyright © 2006-2014 C4Media Inc. InfoQ.com hosted at Contegix, the best ISP we've ever worked with.
Privacy policy
BT