Ruby FFI Brings Native Library Access to JRuby, MRI
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.
Stephanie Davis (nee Stewart) Dec 21, 2014