I can't manage to define a new class (actually an exception) and then
instantiating it from C.
I define the class with:
xr_cException = rb_define_class_under(xr_module, "Exception",
rb_eRuntimeError);
rb_define_method(xr_cException, "initialize",
xr_cException_initialize, 4);
...
static VALUE xr_cException_initialize(VALUE self, VALUE status, VALUE
lasterror, VALUE function, VALUE msg)
{
rb_iv_set(self, "@status", status);
rb_iv_set(self, "@lasterror", lasterror);
rb_iv_set(self, "@function", function);
rb_iv_set(self, "@msg", msg);
return self;
At this point I can write a ruby test program like
a=MYMOD::Exception.new(1,2,3,4)
raise a, "Hello!"
and it works all right and I get output:
Test.rb:5: Hello! (MYMOD::Exception)
However if I try to do the same thing from C with:
VALUE oException, argv[4]={INT2FIX(1), INT2FIX(2), INT2FIX(3),
INT2FIX(4)};
oException = rb_class_new_instance(4, argv, xr_cException);
rb_raise(oException, "Hello!");
I put this piece of code inside a method named raiseit and call test
from a test ruby program. I get error:
Test.rb:9:in `raiseit': undefined method `new' for #<MYMOD::Exception:
MYMOD::Exception> (NoMethodError)
It seems that when I try to create an object of type MYMOD::Exception,
somehow ruby doesn't find the new method. I've tried to define also a
new method for this class, but I get the same error.
By the way, the function rb_call_new_instance seems to be quite
undocumented...
What is the correct way to create a new object of a given class from C?
Thanks
libit
···
--
Posted via http://www.ruby-forum.com/.