Foreign function interface (FFI)
FFI is used to allow a program written in one language invoke functions written in another.
In this example I wrote a small c program that we are going to invoke from a ruby program.
From the ruby side of the world we can use ‘ffi’ to load our c program. First we will need to compile our shared object (.so) file so that we can access it.
Now we can use FFI to define the interface of the c library function that we wish to invoke.
The ffi_lib call loads the libgreeting.so file. attach_function defines a function called greet that accepts a string as an input argument and returns a string.
Now when we run this:
We can also run this with environment variable loaded.
The full source code for this can be found on github.