« Return to Thread: c extension, classes and subclasses

Re: c extension, classes and subclasses

by KUBO Takehiro :: Rate this Message:

Reply to Author | View in Thread

Hi,

On Fri, Jul 10, 2009 at 1:42 AM, Rolando Abarca<funkaster@...> 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(tm) 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".

 « Return to Thread: c extension, classes and subclasses