c - c中ruby Exception类对象的扩展是什么?

标签 c ruby ruby-c-extension

嗨,我在 c 中有以下代码,它是从 ruby​​ 脚本调用的,

static VALUE myMethod(VALUE self, VALUE exc)
{
  int a = TYPE(exc);
  printf(" %d ", a );
  // Some process on exc
}
void Init_myRuby()
{
   VALUE mRuby = rb_define_module("myRuby");
   VALUE mException = rb_define_class_under(mRuby, "Exception", rb_eRuntimeError);
   rb_define_singleton_method(mRuby, "myMethod", myMethod, 4);
}

以下是ruby客户端脚本的代码,

require 'myRuby'
def raiseExc()
exception = myRuby::Exception.new("status","lasterror","function()","Calling some")
myRuby::myMethod(exception, "Exception message: %s, Exception object %d", "Hi from Exception", 100)
end
raiseExc()

我从 ruby​​ 客户端调用 myMethod() 函数。谁能告诉我如何访问c文件中的异常类对象“exc”及其所有属性。

最佳答案

使用 rb_funcall 调用 exc 对象上的方法。

假设 exc 有一个 #description 方法:

VALUE myVar;
myVar = rb_funcall( exc, rb_intern("description"), 0)

如果您需要指定参数:

VALUE myVar;
myVar = rb_funcall( exc, rb_intern("foobar"), 3,
  rb_float_new( 2.5 ),
  INT2FIX( 123 ),
  rb_str_new2("Hello World")
)

关于c - c中ruby Exception类对象的扩展是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7400583/

相关文章:

c - 纯函数的好处

ruby - 确认选择后下拉菜单中的值更改为默认值

c - C 扩展中的 Ruby 关键字参数

c++ - 无法创建 C++ ruby​​ 扩展

c - Ruby C 扩展,如何从段错误中恢复

c - 发现静态库依赖项

c - 在c中绘制图表

ruby-on-rails - rails 中的动态路线

c - 将整数保存到第一个整数给定的数组中

ruby - 我如何确定方法选项的范围?