-
Notifications
You must be signed in to change notification settings - Fork 0
Why use FFI
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.
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 works without changes on Ruby and JRuby (and on any other ruby VM that supports FFI).
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.
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.
Because a FFI extension is easy to write and easy to read, it is also easy to maintain.
A FFI extension doesn't suffer from changes in ruby internal API for creating extensions.