Declaring Class Method with RubyInline

十年热恋 提交于 2019-12-12 00:04:43

问题


I have the following lines of code in my test_func.rb

require 'inline'

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

I am able to call the function only from an object and not class.

I can call it as,

obj = TestFunc.new
obj.testfunc()

But how should i declare so that i can call as,

TestFunc.testfunc()

回答1:


Use Inline::C#c_singleton instead of Inline::C#c:

require 'inline'

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

end

TestFunc.testfunc() # => 0

According to the documentation:

c_singleton: Same as c, but adds a class function.



来源:https://stackoverflow.com/questions/19678586/declaring-class-method-with-rubyinline

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!