C extensions using Singleton

B

Brian Palmer

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
 
T

ts

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

Add this

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


Guy Decoux
 
B

Brian Palmer

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

-- Brian
 
T

ts

B> So just for clarity's sake: using rb_include_module skips the
B> "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
 
B

Brian Palmer

Got it. Thanks.

-- Brian

B> So just for clarity's sake: using rb_include_module skips the
B> "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
 

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

Forum statistics

Threads
474,139
Messages
2,570,807
Members
47,356
Latest member
Tommyhotly

Latest Threads

Top