0

我的代码中有以下几行test_func.rb

require 'inline'

class TestFunc
  inline do |builder|
    builder.c '
      int testfunc() {
        return 0;
      }'
  end
end

我只能从对象而不是类中调用该函数。

我可以称之为,

obj = TestFunc.new
obj.testfunc()

但是我应该如何声明以便我可以调用,

TestFunc.testfunc()
4

1 回答 1

2

使用Inline::C#c_singleton代替Inline::C#c

require 'inline'

class TestFunc
  inline do |builder|
    builder.c_singleton '
    int testfunc() {
      return 0;
    }'
  end

end

TestFunc.testfunc() # => 0

根据文档:

c_singleton:与 c 相同,但增加了一个类函数。

于 2013-11-07T11:47:45.970 回答