-
Notifications
You must be signed in to change notification settings - Fork 335
Why use FFI
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.
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 works without changes on Ruby, JRuby, TruffleRuby, and any other Ruby VM that supports FFI.
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.
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
end
See Basic Usage for other examples.
Because an FFI extension is easy to read and to write, it is also easy to maintain.
An FFI extension isn't broken by changes to the internal Ruby extension API.