【问题标题】:Declaring Class Method with RubyInline使用 RubyInline 声明类方法
【发布时间】:2013-11-09 19:26:36
【问题描述】:

我的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()

【问题讨论】:

  • 我的问题是:testfunc() 是一个 C 函数。不知道如何将其与 ruby​​ 静态方法联系起来。

标签: ruby-on-rails ruby inline


【解决方案1】:

使用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相同,但增加了一个类函数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-29
    • 2013-01-12
    • 2016-12-13
    相关资源
    最近更新 更多