Hello,
I’m trying to create a derived class (ruby class) from a C-implemented
class. Everything works fine, until I tried to add a parameter to the
derived class. When I derive from a pure ruby base class, everything
works as should be, extra parameter or not. Does someone know what is
going on or what I am doing wrong?
I am getting the following output:
gfannes@lt-gfannes help $ ruby test.rb
test.rb:21:in `new’: wrong number of arguments (1 for 0) (ArgumentError)
from test.rb:21
This is the ruby file containing the derivation from the base class. If
bCLib is set to false, things work as should be.
##############test.rb######################
bCLib=true
if bCLib
require(‘base.so’)
else
class Base
def initialize()
puts("base")
end
end
end
class Derived < Base
def initialize(parameter)
puts("init derived")
super()
puts("finished super")
end
end
Derived.new(‘parameter’)
##########################################
This is the C-file that implements the base class in C
######################base.c#####################
#include “ruby.h”
#include <stdio.h>
#include <malloc.h>
typedef struct{
double dValue;
} baseStruct;
VALUE f_new0(VALUE klass)
{
printf(“f_new0.\n”);
VALUE rBase;
baseStruct *pBase=malloc(sizeof(baseStruct));
rBase = Data_Wrap_Struct(klass, 0, free, pBase);
printf(“before rb_obj_call_init.\n”);
rb_obj_call_init(rBase, 0, 0);
printf(“f_new0 finished.\n”);
return rBase;
}
VALUE f_initialize0(VALUE self)
{
printf(“f_initialize0.\n”);
return self;
}
VALUE cBase;
void Init_base()
{
cBase=rb_define_class(“Base”,rb_cObject);
rb_define_singleton_method(cBase, “new”, f_new0, 0);
rb_define_method (cBase, “initialize”, f_initialize0, 0); }
#################################################
Everything is compiled using extconf.rb
#############extconf.rb##########################
require ‘mkmf’
create_makefile(‘base’)
#################################################