According to this site (although the information may be out of date as it is 2009 information) https://zegoggl.es/2009/05/ruby-ffi-recipes.html
When there is a function definition like below
int execvp(const char *file, char *const argv[]);
Ruby-FFI writes:
module Exec
extend FFI::Library
attach_function :execvp, [:string, :pointer], :int
end
When using ...
strptrs = []
strptrs << FFI::MemoryPointer.from_string("vim")
strptrs << FFI::MemoryPointer.from_string("/tmp/foo")
strptrs << nil
# Now load all the pointers into a native memory block
argv = FFI::MemoryPointer.new(:pointer, strptrs.length)
strptrs.each_with_index do |p, i|
argv[i].put_pointer(0, p)
end
Exec.execvp("vim", argv)
If you read it, you'll know what you're doing right away, but it's a little annoying.
If you know a better way, please leave a comment.
That's all for this article.
Recommended Posts