Why use FFI

Amos edited this page May 18, 2013 · 5 revisions

Why should you write your next Ruby extension using ruby-ffi? Why should you consider re-implementing an existing standard extension using ruby-ffi? The section below aims to give you some good answers to these questions.

An FFI extension doesn't need compilation

You don't need a compiler installed on your system to be able to run FFI extensions. Nor do you need to install the development versions of libraries; the runtime versions will do. The libraries you link against will need to have been compiled at some point, of course, but odds are you won't have had to do it.

An FFI extension is multi-platform and multi-implementation

An FFI extension works without changes on Ruby, JRuby, and any other Ruby VM that supports FFI.

An FFI extension is easy to read

Because all the code for an FFI extension is written in Ruby, it's as easy to read as any other Ruby code. There is no need to switch mental modes from C programming to Ruby programming.

An FFI extension is easy to write

Writing a Ruby-FFI based extension is easy thanks to an intuitive DSL. For example, the snippet below is all you need to interface with the 'puts' function defined in the libc C library:

module Foo
  extend FFI::Library
  ffi_lib FFI::Library::LIBC
  attach_function :puts, [ :string ], :int

See Basic Usage for other examples.

An FFI extension is easy to maintain

Because an FFI extension is easy to read and to write, it is also easy to maintain.

An FFI extension is impervious to changes in the internal Ruby API

An FFI extension isn't broken by changes to the internal Ruby extension API.

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.