# C extension, classes and subclasses

**URL:** <https://www.ruby-forum.com/t/c-extension-classes-and-subclasses/171823>\
**Category:** Ruby\
**Created:** [July 9, 2009, 6:42pm UTC](https://www.ruby-forum.com/t/c-extension-classes-and-subclasses/171823 "2009-07-09T18:42:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rolando\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/d9b06d/32.png) [@Rolando\_A](https://www.ruby-forum.com/u/Rolando_A)\
**Post date:** [July 9, 2009, 6:42pm UTC](https://www.ruby-forum.com/t/c-extension-classes-and-subclasses/171823/1 "2009-07-09T18:42:34Z")

</div>

I have a few doubts about how the init mechanism in ruby1.9 (I’m  
working with it right now) works. I have a some classes and  
subclasses, all defined inside a ruby extension, the base class  
defines it’s +new+ method like this:

rb\_define\_singleton\_method(rb\_cMyClass, “new”, rb\_cMyClass\_s\_new, -1);

This is the +new+ method:

VALUE rb\_cMyClass\_s\_new(int argc, VALUE \*argv, VALUE klass) {  
void \*node;  
my\_struct \*ptr;  
…  
VALUE obj = Data\_Make\_Struct(klass, my\_struct, 0, custom\_free, ptr);  
rb\_obj\_call\_init(obj, argc, argv);  
return obj;  
}

and every subclass defines a +new+ method just like that. The thing is:  
Does rb\_obj\_call\_init calls the superclass’ new method?  
Is this The Right Way™ to define/initialise classes and subclasses?

Thanks!

---

<div class="post-metadata">

**Author:** ![Rolando\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/d9b06d/32.png) [@Rolando\_A](https://www.ruby-forum.com/u/Rolando_A)\
**Post date:** [July 12, 2009, 3:10am UTC](https://www.ruby-forum.com/t/c-extension-classes-and-subclasses/171823/2 "2009-07-12T03:10:31Z")

</div>

> and every subclass defines a +new+ method just like that. The thing is:

not if it’s been redefined by the child class, I don’t think.

> Does rb\_obj\_call\_init calls the superclass’ new method?  
> Is this The Right Way™ to define/initialise classes and subclasses?

I think so. Try it out and see if it works 🙂  
=r [maybe asking core might be helpful, too, though…maybe not].

---

<div class="post-metadata">

**Author:** ![Rolando\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/d9b06d/32.png) [@Rolando\_A](https://www.ruby-forum.com/u/Rolando_A)\
**Post date:** [July 12, 2009, 6:18pm UTC](https://www.ruby-forum.com/t/c-extension-classes-and-subclasses/171823/3 "2009-07-12T18:18:28Z")

</div>

Hi,

On Fri, Jul 10, 2009 at 1:42 AM, Rolando A.[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I have a few doubts about how the init mechanism in ruby1.9 (I’m working  
> with it right now) works. I have a some classes and subclasses, all defined  
> inside a ruby extension, the base class defines it’s +new+ method like this:
> 
> rb\_define\_singleton\_method(rb\_cMyClass, “new”, rb\_cMyClass\_s\_new, -1);

Defining a singleton method “new” is old style which had been used for  
ruby 1.6 or before.

> Does rb\_obj\_call\_init calls the superclass’ new method?

No, rb\_obj\_call\_init just calls the instance method ‘initialize’.

> Is this The Right Way™ to define/initialise classes and subclasses?

1. Register a function to allocate a memory region by  
rb\_define\_alloc\_func(rb\_cMyClass, …).
2. Define “initialize” method to initialize the memory region by  
rb\_define\_method(rb\_cMyClass, “initialize”, …).  
It is called by MyClass.new after allocating the memory.
3. Define “initialize\_copy” method also for MyClass#dup and  
MyClass#clone. They don’t call “initialize”.
