开发者

What is extension of ruby Exception class object in c?

HI i have following code in c which is invoked from a ruby script,

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);
}

Following is the code of ruby client script,

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

I invoke myMethod() fu开发者_StackOverflownction from ruby client. Can any one tell me how to access Exception class object "exc" in c file and its all attributes.


Use rb_funcall to call methods on your exc object.

Assuming the exc had a #description method:

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

Of if you need to specify arguments:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜