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:
-
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? -
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).
- 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.