# Define External Superclass in a extension

**URL:** <https://www.ruby-forum.com/t/define-external-superclass-in-a-extension/96270>\
**Category:** Ruby\
**Created:** [June 27, 2006, 8:26pm UTC](https://www.ruby-forum.com/t/define-external-superclass-in-a-extension/96270 "2006-06-27T20:26:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ceaser\_Larry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/87869e/32.png) [@Ceaser\_Larry](https://www.ruby-forum.com/u/Ceaser_Larry)\
**Post date:** [June 27, 2006, 8:26pm UTC](https://www.ruby-forum.com/t/define-external-superclass-in-a-extension/96270/1 "2006-06-27T20:26:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Ceaser\_Larry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/87869e/32.png) [@Ceaser\_Larry](https://www.ruby-forum.com/u/Ceaser_Larry)\
**Post date:** [June 27, 2006, 10:12pm UTC](https://www.ruby-forum.com/t/define-external-superclass-in-a-extension/96270/2 "2006-06-27T22:12:38Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Ceaser\_Larry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/87869e/32.png) [@Ceaser\_Larry](https://www.ruby-forum.com/u/Ceaser_Larry)\
**Post date:** [June 27, 2006, 10:16pm UTC](https://www.ruby-forum.com/t/define-external-superclass-in-a-extension/96270/3 "2006-06-27T22:16:37Z")

</div>

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”)));
