Error from Data_Wrap_Struct when class is inside a module

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

// 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 =

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

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.