Ruby FFI The Foreign Function Interface for accessing system wide shared libraries in Ruby Angel Pizarro email@example.com
Ruby C Extension API Ruby has a C extension mechanism Use SWIG to wrap a C API Non-idiomatic Ruby :( Also IO struct changed between 1.8 and 1.9, SWIG needs new typemap Write your own C modules using this API Other Ruby VM’s out in the cold
Ruby FFI Foreign Function Interface, based on libffi MRI, MacRuby, Rubinius and JRuby support A foreign function interface [...] allows code written in one language to call code written in another language. The libffi library really only provides the lowest, machine dependent layer of a fully featured foreign function interface. A layer must exist above libffi that handles type conversions for values passed between the two languages.
Given a C API : libcairo
Function Input Args That sucks! We lose all method signature information!
Alias a type: still sucks Types, Enums & Method sig Const now becomes nicer: ants
Managed Structs DANGER WILL ROBINSON! Only use if you REALLY want Ruby GC to manage references and memory
Final Notes Beware library calls that open STIN / STDOUT to buffer writes Specifically, they may close the socket and Ruby IO becomes FUBAR Multi-dimensional arrays in structs not supported (“unroll” the previous dimension) Do not use ManagedStruct if you just want type casting on a pointer ref Essentially still using C idioms to code (e.g. memory management)