Define External Superclass in a extension

I’m trying to learn how to create a ruby extension. I’ve read the
pick axe book but I’m running into a problem understanding how to get
a reference to classes defined outside the ruby.h header.

My problem is how do I reference the DBI::BaseDriver as a super class
to my object?

In C I understand I can use the following to define a new class

VALUE rb_cMyClass = rb_define_class(“MyClass”, rb_cObject)

somehow I need to replace rb_cObject with a reference to the
DBI::BaseDriver like so

VALUE rb_cMyClass = rb_define_class(“MyClass”, [DBI::BaseDriver])

Any help or nudge in the right direction would be appreciated.

Thanks
Ceaser

This has worked for me in the past …

VALUE tmp, your_class;

rb_require( “dbi” );

tmp = rb_const_get( rb_cObject, rb_intern(“DBI”) );
tmp = rb_const_get( tmp, rb_intern(“BaseDriver”) );

your_class = rb_define_class( “YourClass”, tmp );

The rb_require used above will not work for gems. You’ll have to call
the require method of the Gem module to load gems.

Blessings,
Tim P.

On Jun 27, 2006, at 2:23 PM, Ceaser Larry wrote:

somehow I need to replace rb_cObject with a reference to the
DBI::BaseDriver like so

VALUE rb_cMyClass = rb_define_class(“MyClass”, [DBI::BaseDriver])

Any help or nudge in the right direction would be appreciated.

Thanks
Ceaser

See rb_const_get. It’ll be something like

rb_cMyClass = rb_define_class(“MyClass”,
rb_const_get(
rb_const_get(rb_cObject, intern(“DBI”)),
intern(“BaseDriver”)));

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs