ruby ffiを使用して、関数をアタッチする共有オブジェクト lib があります。各関数にエイリアスを付けて、エイリアスを非公開にしたいのは、それらを呼び出すと危険な場合があるためです。各関数を独自の ruby モジュール関数にラップしています。簡単な例を次に示します。
module LibC
extend FFI::Library
ffi_lib FFI::Library::LIBC
attach_function :free, [:pointer], :void
end
module MyModule
class << self
extend FFI::Library
ffi_lib '../my_shared_lib.so'
def function(str)
is_string(str)
ptr = ffi_function(str)
result = String.new(ptr.read_string)
LibC.free(ptr)
result
end
private
# attach function
attach_function :ffi_function, :function, [:string], :pointer
def is_string(object)
unless object.kind_of? String
raise TypeError,
"Wrong argument type #{object.class} (expected String)"
end
end
end
end
この関数ffi_function
は、モジュールの外部でも呼び出すことができるようです。どうすれば完全に非公開にできますか?