InfoQ Homepage News Ruby FFI Brings Native Library Access to JRuby, MRI
Ruby FFI Brings Native Library Access to JRuby, MRI
Leia em PortuguÃas
This item in japanese
Dec 10, 2008 1 min read
Write for InfoQ
Feed your curiosity. Help 550k+ globalsenior developers
each month stay ahead.Get in touch
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.
This content is in the Scripting topic
Related Topics:
-
Related Editorial
-
Related Sponsors
-
Popular across InfoQ
-
AWS Introduces ECS Managed Instances for Containerized Applications
-
Producing a Better Software Architecture with Residuality Theory
-
GitHub Introduces New Embedding Model to Improve Code Search and Context
-
Google DeepMind Introduces CodeMender, an AI Agent for Automated Code Repair
-
Building Distributed Event-Driven Architectures across Multi-Cloud Boundaries
-
Elena Samuylova on Large Language Model (LLM)-Based Application Evaluation and LLM as a Judge
-
Related Content
The InfoQ Newsletter
A round-up of last week’s content on InfoQ sent out every Tuesday. Join a community of over 250,000 senior developers. View an example