From: nobu.nokada@... Date: 2001-11-19T10:55:14+09:00 Subject: [ruby-talk:25807] Re: Subclassing native struct oddity --Multipart_Mon_Nov_19_10:52:36_2001-1 Content-Type: text/plain; charset=US-ASCII At Mon, 19 Nov 2001 06:16:09 +0900, Kero van Gelder wrote: > [2 native.c ] > #include > > VALUE rb_cBla; > > VALUE bla_new(VALUE module) { > int *b = ALLOC(int); > VALUE bla; > *b = 7; > bla = Data_Wrap_Struct(rb_cBla, NULL, free, b); > return bla; > } You'd better to define Bla#initialize not but Bla.new only, or Blaat#initialize would never be called. > void Init_native(void) { > rb_cBla = rb_define_class("Bla", rb_cObject); > rb_define_module_function(rb_cBla, "new", bla_new, 0); > } And bla_new() shouldn't be a module function but a singleton method. Of course, as David wrote, hardcoded rb_cBla is the problem. With 1.7 feature, new allocation framework, see attachments. --Multipart_Mon_Nov_19_10:52:36_2001-1 Content-Type: text/x-csrc; charset=US-ASCII Content-Disposition: attachment; filename="native.c" Content-Transfer-Encoding: 7bit #include VALUE rb_cBla; #if HAVE_OBJECT_ALLOCATE static VALUE bla_allocate(VALUE klass) { return Data_Wrap_Struct(klass, NULL, free, ALLOC(int)); } #else static VALUE bla_new(int argc, VALUE *argv, VALUE klass) { VALUE bla = Data_Wrap_Struct(klass, NULL, free, ALLOC(int)); rb_obj_call_init(bla, argc, argv); return bla; } #endif static VALUE bla_initialize(VALUE bla) { int *b; Data_Get_Struct(bla, int, b); *b = 7; return bla; } void Init_native(void) { rb_cBla = rb_define_class("Bla", rb_cObject); #if HAVE_OBJECT_ALLOCATE rb_define_singleton_method(rb_cBla, "allocate", bla_allocate, 0); #else rb_define_singleton_method(rb_cBla, "new", bla_new, -1); #endif rb_define_method(rb_cBla, "initialize", bla_initialize, 0); } --Multipart_Mon_Nov_19_10:52:36_2001-1 Content-Type: text/x-ruby; charset=US-ASCII Content-Disposition: attachment; filename="extconf.rb" Content-Transfer-Encoding: 7bit require 'mkmf' $CPPFLAGS += " -DHAVE_OBJECT_ALLOCATE" if Object.respond_to? :allocate create_makefile("native") --Multipart_Mon_Nov_19_10:52:36_2001-1 Content-Type: text/plain; charset=US-ASCII Nobu Nakada --Multipart_Mon_Nov_19_10:52:36_2001-1--