From: nobu.nokada@... Date: 2002-04-23T17:51:32+09:00 Subject: Re: building extensions-- new vs initialize Hi, At Tue, 23 Apr 2002 10:09:52 +0900, Norman Makoto Su wrote: > Hi, I'm trying to build a ruby extension in C. While looking at the pickaxe CD > player example, I noticed that they define a singleton_method called "new" and > then a normal method called "initialize". What is the motivation for this? Is > it so that when you call CDPlayer.new, the returned object is of type Data? > (this can't be achieved with just initialize?) In general, redefinition of "new" will be deprecated in 1.8. Instead, you have to do are: (1) define "allocate" class method, (2) define "initialize" instance method, (3) call rb_call_super() in "initialize", and (4) call rb_enable_super() for "initialize" in Init_xxx(). Although 3 and 4 are not necessary, I recommend them. I hope rough/ext/stringio.c to be an example. > I'm confused as to when it is or not neccessary to define a "new" method in > addition to an "initialize" method. For example, when defining class variables, > does this have to be done in a "new" method, since the class has not been > instantiated? No, once defined a class, you can define class variables whenever. Typically in the Init function, "new" is called for each object, however class variables are singleton. -- Nobu Nakada