Error from Data_Wrap_Struct when class is inside a module

M

Michael Zatopek

I'm playing around with writing a ruby extension in C and I'm getting
a "wrong argument type Class (expected Class) (TypeError)" error that
appears to be coming from my Data_Wrap_Struct call.

Parred down C code:

VALUE Library_new(VALUE klass)
{
my_struct* lib;
VALUE new_inst;

// C fuction to allocate and initialize lib
init(lib);

// create new ruby object around C structure (Library_free defined
but not shown)
new_inst = Data_Wrap_Struct(klass, 0, Library_free, lib);

return new_inst;
}

void Init_extension() {
VALUE classLibrary = Qnil;

classLibrary = rb_define_class("Library", rb_cObject);
rb_define_singleton_method(classLibrary, "new", Library_new, 0);
}

And in ruby:

require 'extension'

lib = Library.new

Now, in the process of debugging and trying to put in some debug
statements, I have discovered that by adding the line:

printf("class is a: %s\n", STR2CSTR(rb_funcall(rb_funcall
(klass,rb_intern("class"),0),rb_intern("to_s"),0)));

just before the Data_Wrap_Struct call, then I don't get the error and
appear to have an initialized library. Why would calling the
equivalent of a 'self.class.to_s' change anything? I'm running "ruby
1.8.7 (2010-12-23 patchlevel 330) [powerpc-darwin8]". Any help or
suggestions are much appreciated.

Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top