C extensions using Singleton

Another C extension question for the experts: in a C ruby class, I'm trying to use the Singleton standard library. It doesn't work, though no errors are thrown.

  ruby_rpg_class = rb_define_class("RPGEngine", rb_cObject);
  rb_require("singleton");
  rb_include_module(ruby_rpg_class, rb_const_get(rb_cObject, rb_intern("Singleton")));

With this C code above, I can still call RPGEngine.new and get a new instance of the class. RPGEngine.instance throws a NoMethodError.

Is the code above correct?

Thanks again,
-- Brian

  ruby_rpg_class = rb_define_class("RPGEngine", rb_cObject);
  rb_require("singleton");
  rb_include_module(ruby_rpg_class, rb_const_get(rb_cObject,
rb_intern("Singleton")));

Add this

    rb_funcall(rb_const_get(rb_cObject, rb_intern("Singleton")),
               rb_intern("included"), 1, ruby_rpg_class);

Guy Decoux

So just for clarity's sake: using rb_include_module skips the "included" callback that usually happens when a module is included?

-- Brian

ts wrote:

···

"B" == Brian Palmer <brian@pocketmartiansoftware.com> writes:
           
  ruby_rpg_class = rb_define_class("RPGEngine", rb_cObject);
  rb_require("singleton");
  rb_include_module(ruby_rpg_class, rb_const_get(rb_cObject, rb_intern("Singleton")));

Add this

   rb_funcall(rb_const_get(rb_cObject, rb_intern("Singleton")),
              rb_intern("included"), 1, ruby_rpg_class);

Guy Decoux

So just for clarity's sake: using rb_include_module skips the
"included" callback that usually happens when a module is included?

No, not really

Module#include call #append_features and #included

rb_include_module() is the default function for Module#append_features

Guy Decoux

Got it. Thanks.

-- Brian

ts wrote:

···

"B" == Brian Palmer <brian@pocketmartiansoftware.com> writes:
           
So just for clarity's sake: using rb_include_module skips the "included" callback that usually happens when a module is included?

No, not really

Module#include call #append_features and #included

rb_include_module() is the default function for Module#append_features

Guy Decoux