I'm developing a Ruby-C++ extension. I have to write a non-static method in a CPP class and I have to invoke that class method in ruby client by using the class instance.
Following is the main.cpp:
#include "ruby.h"
#include <iostream>
using namespace std;
class Mclass
{
public:
int i;
static VALUE newMethod(VALUE self);
static VALUE newInitialize(VALUE self);
};
VALUE Mclass::newMethod(VALUE self)
{
cout<<"It is newMethod() method of class Mclass"<< endl;
return Qnil;
}
VALUE Mclass::newInitialize(VALUE self)
{
cout<<"It is newInitialize() method of class Mclass"<< endl;
return Qnil;
}
extern "C" void Init_Test(){
VALUE lemon = rb_define_module("Test");
VALUE mc = rb_define_class_under(lemon, "Mclass", rb_cObject);
rb_define_method(mc, "new",
reinterpret_cast< VALUE(*)(...) >(Mclass::newMethod), 0);
rb_define_method(mc, "initialize",
reinterpret_cast< VALUE(*)(...) >(Mclass::newInitialize), 0);
}
Also following is the ruby client code:
require 'Test'
include Test
a = Mclass.new
I'm able to get instance of "Mclass" in ruby client. But want invoke the class non-static method in the ruby client. How can I add the non-static method in CPP class?