I am attempting to write a Ruby extension in C and am having an issue calling a method of a object that is passed into the extension.
For example, I have a ruby class defined as follows:
class MyClass
def write(message)
...do a write...
end
end
The extension is defined as:
#include "ruby.h"
static VALUE _init(VALUE self, VALUE logger)
{
switch (TYPE(logger))
{
case T_OBJECT:
ID method = rb_intern("write");
if (rb_respond_to(logger, method))
fputs("logger responds to write\n", stderr);
else
fputs("logger does NOT respond to write\n", stderr);
break;
default:
fputs("logger not an object\n", stderr);
}
return self;
}
void Init_MyExt()
{
VALUE MyExt_class = rb_define_class("MyExt", rb_cObject);
rb_define_method(MyExt_class, "initialize", _init, 1);
}
The ruby script doing the call looks similar to:
require 'MyExt.so'
@log = MyClass.new()
@myext = MyExt(@log)
The response I get is : "logger does NOT respond to write"
I am sure I am missing some subtle issue here, as I would have expected the logger to be able to respond to the write request.
In the back of my mind, I feel that the 'VALUE logger' parameter is only describing a pointer and not describing logger as an object with methods.
Any insights are greatly appreciated. Thanks!