Skip to content
akzhan edited this page Apr 9, 2012 · 2 revisions

Why should you write your next ruby extension using ruby-ffi? Why should you consider to re-implement some existing standard extensions using ruby-ffi? The sections below aim to give you some good reason to do that.

A FFI extension doesn't need compilation

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

A FFI extension is multi-platform and multi-implementation

A FFI extension works without changes on Ruby and JRuby (and on any other ruby VM that supports FFI).

A FFI extension is easy to read

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

A 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 'puts' function defined in the libc C library:

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

See Basic Usage for other examples.

A FFI extension is easy to maintain

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

A FFI extension is impervious to changes in ruby internal API

A FFI extension doesn't suffer from changes in ruby internal API for creating extensions.