# Subclassing in a C extension

**URL:** <https://www.ruby-forum.com/t/subclassing-in-a-c-extension/181080>\
**Category:** Ruby\
**Created:** [January 5, 2010, 6:07am UTC](https://www.ruby-forum.com/t/subclassing-in-a-c-extension/181080 "2010-01-05T06:07:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ericrchr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/8baadc/32.png) [@ericrchr](https://www.ruby-forum.com/u/ericrchr)\
**Post date:** [January 5, 2010, 6:07am UTC](https://www.ruby-forum.com/t/subclassing-in-a-c-extension/181080/1 "2010-01-05T06:07:04Z")

</div>

I’ve been trying to make a subclass written in C of the MySQL/Ruby  
class (mysql gem), which is also a C extension. I’m running into some  
problems and questions and I can’t seem to find anything explaining  
the right things to do.

Here is the source of the beginning of my init function:

void Init\_mysqlemb(void)  
{  
// Debugging class path  
// print\_ruby\_string\_array is a function I wrote to print an array  
printf(“Class path: “);  
print\_ruby\_string\_array(rb\_gv\_get(”$:”));

// This doesn’t load the mysql extension as it should  
//rb\_require(“mysql”);

// This file is at  
/Library/Ruby/Site/1.8/universal-darwin10.0/require\_test.rb;  
// it loads just fine.  
rb\_require(“require\_test”);

// This doesn’t work either (I saw somewhere in the docs or one of  
the Ruby source files that you can specify the full filename of the  
extension binary. I guess the whole path is required.  
//rb\_require(“mysql\_api.bundle”);

//rb\_require("/Users/eric/.gem/ruby/1.8/gems/mysql-2.8.1/ext/mysql\_api/mysql\_api.bundle");  
//  
Works  
//rb\_require("/Users/eric/.gem/ruby/1.8/gems/mysql-2.8.1/lib/mysql\_api.bundle");  
//  
Works  
//rb\_require("/Users/eric/.gem/ruby/1.8/gems/mysql-2.8.1/lib/mysql.rb");  
//  
Works

// Simplest and seemingly most platform-agnostic way that actually  
works  
rb\_eval\_string(“require ‘mysql’”);

//VALUE cMysql = rb\_const\_get(rb\_cObject, rb\_intern(“Mysql”));  
VALUE cMysql= rb\_path2class(“Mysql”);

// Debugging superclass  
printf(“Superclass: %s\n”, rb\_class2name(cMysql));

cMysqlemb = rb\_define\_class(“Mysqlemb”, cMysql);

…  
}

My questions:

1. The original Mysql class is installed as a gem. Should my extension  
use rb\_require(“rubygems”)? Or is requiring rubygems the  
responsibility of the scripts that use it?

2. Why doesn’t rb\_require(“mysql”) work? In a script or irb, I am able  
to load it just fine via require; rb\_eval\_string(“require ‘mysql’”)  
also works in C, but that seems like a hack. I’ve compared my $: path  
in irb and inside the extension, and they are identical.

- Someone on IRC said maybe rb\_require works only with .rb files; but  
then I noticed that the Mysql extension does include a file mysql.rb.  
That file, in turn, requires mysql\_api.bundle, which is the actual  
binary (on Mac OS X).

1. Is there one best way to get the class object for a class specified  
by a C string? I’ve seen both rb\_const\_get(rb\_cObject,  
rb\_intern(“Classname”)) and rb\_path2class(“Classname”) and wonder if  
they have any practical differences.

Thanks.

---

<div class="post-metadata">

**Author:** ![ericrchr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/8baadc/32.png) [@ericrchr](https://www.ruby-forum.com/u/ericrchr)\
**Post date:** [January 5, 2010, 10:36am UTC](https://www.ruby-forum.com/t/subclassing-in-a-c-extension/181080/2 "2010-01-05T10:36:55Z")

</div>

Eric C. wrote:

> 1. The original Mysql class is installed as a gem. Should my extension  
> use rb\_require(“rubygems”)? Or is requiring rubygems the  
> responsibility of the scripts that use it?

In my opinion, you should not use rb\_require(“rubygems”). If your  
extension is packaged as a gem, then clearly rubygems will be enabled  
already; if your extension is being loaded outside rubygems, then I  
think it’s the user’s responsibility to require rubygems.

It annoys me when I build a system where all the libraries are  
‘vendorised’ locally, and one of them insists on doing a require  
‘rubygems’ which I neither need nor want. But to be fair, this is a  
mistake I’ve made myself in the past.

> 1. Why doesn’t rb\_require(“mysql”) work?

You haven’t shown the exception, but my guess is that rb\_require hooks  
in at a lower level which bypasses all the fudging that rubygems does  
with load\_paths.

You could prove this using something like (untested):

rb\_require(‘rubygems’);  
rb\_eval\_string(‘gem “mysql”’);  
rb\_require(‘mysql’);

However that defeats (1).

It might be cleaner to use one of the rb\_funcall variants to to  
send(:require,“mysql”) instead of rb\_eval\_string.

> 1. Is there one best way to get the class object for a class specified  
> by a C string? I’ve seen both rb\_const\_get(rb\_cObject,  
> rb\_intern(“Classname”)) and rb\_path2class(“Classname”) and wonder if  
> they have any practical differences.

Looking at the source of rb\_path2class (in variable.c), it has built-in  
ability to follow namespaces, i.e. rb\_path2class(“Foo::Bar”) should  
work. Otherwise, you can see it just does rb\_const\_get\_at itself,  
starting at rb\_cObject.

---

<div class="post-metadata">

**Author:** ![ericrchr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/8baadc/32.png) [@ericrchr](https://www.ruby-forum.com/u/ericrchr)\
**Post date:** [January 6, 2010, 5:57am UTC](https://www.ruby-forum.com/t/subclassing-in-a-c-extension/181080/3 "2010-01-06T05:57:06Z")

</div>

Eric C. wrote:

> My questions:
> 
> 1. The original Mysql class is installed as a gem. Should my extension  
> use rb\_require(“rubygems”)? Or is requiring rubygems the  
> responsibility of the scripts that use it?
> 
> 2. Why doesn’t rb\_require(“mysql”) work? In a script or irb, I am able

Here’s a simple solution: ship a mysqlemb.rb file with your extension,  
to be required by programmers. This file will “require ‘mysql’”, then  
will “require ‘mysqlemv\_ext’” to load the C extension.

---

<div class="post-metadata">

**Author:** ![ericrchr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/8baadc/32.png) [@ericrchr](https://www.ruby-forum.com/u/ericrchr)\
**Post date:** [January 6, 2010, 6:45am UTC](https://www.ruby-forum.com/t/subclassing-in-a-c-extension/181080/4 "2010-01-06T06:45:39Z")

</div>

On Tue, Jan 5, 2010 at 3:36 AM, Brian C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> It annoys me when I build a system where all the libraries are  
> ‘vendorised’ locally, and one of them insists on doing a require  
> ‘rubygems’ which I neither need nor want. But to be fair, this is a  
> mistake I’ve made myself in the past.

That makes sense. It is possible to install mysql (and I would imagine  
all or most other gems) directly in a site directory, rather than as  
gems, so it doesn’t make sense to require rubygems outright.

> rb\_eval\_string(‘gem “mysql”’);  
> rb\_require(‘mysql’);
> 
> However that defeats (1).
> 
> It might be cleaner to use one of the rb\_funcall variants to to  
> send(:require,“mysql”) instead of rb\_eval\_string.

Oh, thanks for that. I would send that to the _main_ object. After  
some digging, it appears that that is assigned to the variable  
ruby\_top\_self (which isn’t declared in a header), so I did this:

```
EXTERN VALUE ruby_top_self;
...
rb_funcall(ruby_top_self, rb_intern("send"), 2,

```

ID2SYM(rb\_intern(“require”)), rb\_str\_new2(“mysql”));

This also works:

```
rb_funcall(ruby_top_self, rb_intern("require"), 1, 

```

rb\_str\_new2(“mysql”));

Why would one want to use send rather that calling require directly?

> > 1. Is there one best way to get the class object for a class specified  
> > by a C string? I’ve seen both rb\_const\_get(rb\_cObject,  
> > rb\_intern(“Classname”)) and rb\_path2class(“Classname”) and wonder if  
> > they have any practical differences.
> 
> Looking at the source of rb\_path2class (in variable.c), it has built-in  
> ability to follow namespaces, i.e. rb\_path2class(“Foo::Bar”) should  
> work. Otherwise, you can see it just does rb\_const\_get\_at itself,  
> starting at rb\_cObject.

OK.

Thanks!

---

<div class="post-metadata">

**Author:** ![ericrchr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/8baadc/32.png) [@ericrchr](https://www.ruby-forum.com/u/ericrchr)\
**Post date:** [January 6, 2010, 9:24am UTC](https://www.ruby-forum.com/t/subclassing-in-a-c-extension/181080/5 "2010-01-06T09:24:17Z")

</div>

Eric C. wrote:

> Oh, thanks for that. I would send that to the _main_ object. After  
> some digging, it appears that that is assigned to the variable  
> ruby\_top\_self (which isn’t declared in a header), so I did this:
> 
> ```
> EXTERN VALUE ruby_top_self;
> ...
> rb_funcall(ruby_top_self, rb_intern("send"), 2,
> 
> ```
> 
> ID2SYM(rb\_intern(“require”)), rb\_str\_new2(“mysql”));
> 
> This also works:
> 
> ```
> rb_funcall(ruby_top_self, rb_intern("require"), 1, 
> 
> ```
> 
> rb\_str\_new2(“mysql”));
> 
> Why would one want to use send rather that calling require directly?

Sorry, I meant the latter - using rb\_funcall to invoke ‘require’, a bit  
like a ‘send’ in ruby, but not actually invoking the ‘send’ method.

I don’t know if there’s a cleaner way to avoid the hidden ruby\_top\_self.
