# Inheritance from C object with different parameter count

**URL:** <https://www.ruby-forum.com/t/inheritance-from-c-object-with-different-parameter-count/53242>\
**Category:** Ruby\
**Created:** [January 30, 2006, 10:14am UTC](https://www.ruby-forum.com/t/inheritance-from-c-object-with-different-parameter-count/53242 "2006-01-30T10:14:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Geert\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/d26b3c/32.png) [@Geert\_F](https://www.ruby-forum.com/u/Geert_F)\
**Post date:** [January 30, 2006, 10:14am UTC](https://www.ruby-forum.com/t/inheritance-from-c-object-with-different-parameter-count/53242/1 "2006-01-30T10:14:15Z")

</div>

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’)

#################################################

---

<div class="post-metadata">

**Author:** ![Geert\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/d26b3c/32.png) [@Geert\_F](https://www.ruby-forum.com/u/Geert_F)\
**Post date:** [January 30, 2006, 9:46pm UTC](https://www.ruby-forum.com/t/inheritance-from-c-object-with-different-parameter-count/53242/2 "2006-01-30T21:46:57Z")

</div>

On Mon, Jan 30, 2006 at 06:12:13PM +0900, Geert F. wrote:

> 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?

```
Hmmm... weird. If you comment the "super()" call in 

```

Derived#initialize, it  
dies with the exact same error. In fact, you don’t even get the “init  
derived”  
message.

```
After investigating a little, I found out that changing the 

```

definition of  
new to:

```
rb_define_singleton_method(cBase, "new", f_new0, -1);

```

makes it work. I don’t know if that’s entirely correct (I’m not very  
familiar  
with C extensions), but it works 🙂 I guess the problem is that, as you  
don’t  
redefine the “new” method in the class Derived, you get the Base one, so  
it  
chokes. I don’t know if the usual thing here is always defining “new”  
methods  
with any parameter count…

```
HTH,
```

---

<div class="post-metadata">

**Author:** ![Geert\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/d26b3c/32.png) [@Geert\_F](https://www.ruby-forum.com/u/Geert_F)\
**Post date:** [February 1, 2006, 11:46pm UTC](https://www.ruby-forum.com/t/inheritance-from-c-object-with-different-parameter-count/53242/3 "2006-02-01T23:46:33Z")

</div>

“Geert F.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> 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?

Your C code does not do:

class Base  
def initialize()  
puts(“base”)  
end  
end

It’s more like:

class Base  
def self.new  
…  
end  
def self.initialize  
…  
end  
end

Thus, Derived.new(arg) is an error, since it calls Base.new, which  
you’ve defined to take no args.

Note that Blah.new is a completely different method to  
Blah#initialize. Blah.new would be a singleton method of the Class  
instance Blah, but you usually don’t want that, as the standard  
Class#new is sufficiently magical to do everything you need. In  
pseudoruby, it looks like:

class Class  
def new(\*args)  
object = self.alloc\_func  
object.initialize(\*args)  
return object  
end  
end

Object#dup and Object#clone are similar:

class Object  
def dup(other)  
object = self.alloc\_func  
object.initialize\_copy(other)  
return object  
end  
def clone(other)  
object = self.alloc\_func  
object.singleton\_class = other.singleton\_class.copy  
# … some other diddly bits …  
object.initialize\_copy(other)  
return object  
end  
end

So what you normally do is:

– define the alloc func to alloc a ruby object from heap memory:  
VALUE Thingy\_allocate(VALUE klass) {  
struct Thingy \*thingy;  
return Data\_Make\_Struct(klass, struct Thingy,  
Thingy\_mark, Thingy\_free, thingy);  
}

– define an #initialize method:  
VALUE Thingy\_initialize(VALUE self, …args…) {  
…  
}

– define an #initialize\_copy method:  
VALUE Thingy\_initialize\_copy(VALUE self, VALUE other) {  
…  
}

– bind 'em:  
void Init\_libthingies {  
VALUE cThingy;  
cThingy = rb\_define\_class(“Thingy”, rb\_cObject);  
rb\_define\_alloc\_func(cThingy, Thingy\_allocate);  
rb\_define\_method(cThingy, “initialize”, Thingy\_initialize,  
num\_args);  
rb\_define\_method(cThingy, “initialize”, Thingy\_initialize\_copy,  
1);  
…  
}

Of course, #initialize\_copy is optional (in fact, so is #initialize),  
but the point is that doing things this way means you only have to do  
the malloc-ing once (in the alloc func), and everywhere else you’re  
dealing with a perfectly valid ruby object. No need to call  
#initialize or obj\_call\_init() or worry about your object not being  
allocated if #initialize is overriden or gets interrupted by an  
exception… just let the standard methods call your hooks and lie  
back. 🙂
