0

嗨,我在 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”及其所有属性。

4

1 回答 1

1

用于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")
)
于 2012-03-08T13:47:00.683 回答