使用RubyInline声明类方法 [英] Declaring Class Method with RubyInline

查看:241
本文介绍了使用RubyInline声明类方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的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.

我可以这样称呼,

obj = TestFunc.new
obj.testfunc()

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

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

TestFunc.testfunc()

推荐答案

使用 Inline::C#c :

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

根据文档:

c_singleton :与c相同,但是添加了一个类函数.

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

这篇关于使用RubyInline声明类方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆