我有一个使用 ruby ffi附加函数的共享对象库。我想为每个函数附加一个别名并将别名设为私有,因为调用它们可能很危险。我将每个函数包装在它们自己的 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
似乎仍然可以在模块外部调用。我怎样才能让它完全私有化?