I am currently working on a C string metric library and am writing bindings for ruby. Using ffi how can I attach a function with a signature like char *function(const char *, const char *)? The function in question will allocate a string on the heap with malloc and then return a pointer to that string.
I believe I will need to wrap the ffi-attached function in a ruby method so that I can convert the returned string pointer to a ruby string and release the old pointer.
After a bit of work and messing around in
irb, I figured out how to safely wrap a C function that returns achar *. First it is necessary to wraplibc'sfreefunction.Now that we have access to
free, we can attach the function and wrap it in a ruby module function. I also included a helper method to check for valid string parameters.That's it, hope it helps anyone else who has a similar problem.