From: Daniel Berger Date: 2002-05-12T06:45:05+09:00 Subject: Re: Ruby extensions - pushing classes into other modules ts wrote: >>>>>>"D" == Daniel Berger writes: >>>>>> > > D> I want to create a base class called "Sys" that would simply serve as a > ^^^^^ > module, no ? Oops. Yes, right. > > D> would not only help reduce serious bloat in one file, it would allow > D> others to easily add their own extensions under the 'Sys' namespace. > > Now if I want to extend the Sys namespace with my private class Sys::Own, > I'll write in the file own.c > > void Init_own() > { > VALUE sys_mSys, own_cOwn; > rb_require("sys"); > sys_mSys = rb_const_get(rb_cObject, rb_intern("Sys")); > own_cOwn = rb_define_class_under(sys_mSys, "Own", rb_cObject); > /* ... */ > } I took this approach first, since I figured it was only fair that I make my modules the way everyone else will have to. :) Here's the file - Yes, I know it's silly to have one method - I'm just testing. :) /* Sys.c */ #include "ruby.h" #ifdef __cplusplus extern "C" #endif VALUE mSys; void Init_Sys() { mSys = rb_define_module("Sys"); } //////////////////////////////////////// /* Info.c */ #include "ruby.h" #include #ifdef __cplusplus extern "C" #endif #ifndef MAXHOSTNAMELEN #define MAXHOSTNAMELEN 256 #endif VALUE mSys, cInfo; static VALUE sys_info_hostname() { char h[MAXHOSTNAMELEN]; int rv; rv = gethostname(h,MAXHOSTNAMELEN); return rb_str_new2(h); } void Init_Info() { rb_require("Sys"); mSys = rb_const_get(rb_cObject,rb_intern("Sys")); cInfo = rb_define_class_under(mSys,"Info",rb_cObject); rb_define_module_function(cInfo,"hostname",sys_info_hostname,0); } It makes fine, but when I try this: require 'Sys' puts Sys::Info.hostname I get this error: ../test.rb:9: uninitialized constant Info at Sys (NameError) Am I just building it wrong? I built Sys first, then Inf with a simple "create_makefile" command, separately. Thanks. Dan