# Multiple constructors in a .c extension?

**URL:** <https://rubytalk.org/t/multiple-constructors-in-a-c-extension/2343>\
**Category:** ruby-talk\
**Created:** [17 October 2002 10:46 UTC](https://rubytalk.org/t/multiple-constructors-in-a-c-extension/2343 "2002-10-17T10:46:45Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Paul\_E.C\_Melis](https://avatars.discourse-cdn.com/v4/letter/p/c4cdca/32.png) [@Paul\_E.C\_Melis](https://rubytalk.org/u/Paul_E.C_Melis)\
**Post date:** [17 October 2002 10:46 UTC](https://rubytalk.org/t/multiple-constructors-in-a-c-extension/2343/1 "2002-10-17T10:46:45Z")

</div>

Hello,  
I was wondering if it is wise/common practice/allowed to have multiple  
constructors for a class defined in a C extension.  
Specifically, I’m currently doing the following in my extension:

rb\_define\_singleton\_method(c…, “new”, t\_new, 1);  
rb\_define\_singleton\_method(c…, “new\_from\_file”, t\_new\_from\_file, 1);

With both calls a structure is allocated and wrapped with  
Data\_Wrap\_Struct(), followed by rb\_obj\_call\_init() on the created object.

The idea of course is to be able to say  
obj1 = MyClass.new(…)  
and  
obj2 = MyClass.new\_from\_file(‘some\_file’)  
in my script.

The defined initialize method in the extension actually does nothing, so I  
would rather leave it out. I don’t see any reason myself why object  
construction and initialization can’t be done completely by the C functions  
implementing new and new\_from\_file. Is there anything special about the  
"new" singleton method other than that it implicitly calls “initialize”?

Thanks,  
Paul

---

<div class="post-metadata">

**Author:** ![Enric\_Lafont](https://avatars.discourse-cdn.com/v4/letter/e/e8c25b/32.png) [@Enric\_Lafont](https://rubytalk.org/u/Enric_Lafont)\
**Post date:** [17 October 2002 11:09 UTC](https://rubytalk.org/t/multiple-constructors-in-a-c-extension/2343/2 "2002-10-17T11:09:37Z")

</div>

Paul E.C. Melis wrote:

> Hello,  
> I was wondering if it is wise/common practice/allowed to have multiple  
> constructors for a class defined in a C extension.  
> Specifically, I’m currently doing the following in my extension:
> 
> rb\_define\_singleton\_method(c…, “new”, t\_new, 1);  
> rb\_define\_singleton\_method(c…, “new\_from\_file”, t\_new\_from\_file, 1);
> 
> With both calls a structure is allocated and wrapped with  
> Data\_Wrap\_Struct(), followed by rb\_obj\_call\_init() on the created object.
> 
> The idea of course is to be able to say  
> obj1 = MyClass.new(…)  
> and  
> obj2 = MyClass.new\_from\_file(‘some\_file’)  
> in my script.
> 
> The defined initialize method in the extension actually does nothing, so I  
> would rather leave it out. I don’t see any reason myself why object  
> construction and initialization can’t be done completely by the C functions  
> implementing new and new\_from\_file. Is there anything special about the  
> “new” singleton method other than that it implicitly calls “initialize”?

This brings another question, why does ruby use as default an instance  
method (initialize) for doing what must be a class method (new). I think  
that the current approach, does not follow the least surprise principle.

You can of course do:

class Test  
def Test.new\_from\_file(‘someFile’)  
aTempVariable=Test.new # because it seems to be the only  
contructor allowed  
# … whatever other things this routine does  
return aTempVariable  
end  
end

This way you can then

aTestVar = Test.new\_from\_file(‘someFile’)

But it’s unnatural and strange (at least for me …)

Enric

---

<div class="post-metadata">

**Author:** ![Nobuyoshi\_Nakada](https://avatars.discourse-cdn.com/v4/letter/n/edb3f5/32.png) [@Nobuyoshi\_Nakada](https://rubytalk.org/u/Nobuyoshi_Nakada)\
**Post date:** [17 October 2002 12:11 UTC](https://rubytalk.org/t/multiple-constructors-in-a-c-extension/2343/3 "2002-10-17T12:11:50Z")

</div>

Hi,

> **···**
>
> At Thu, 17 Oct 2002 19:46:45 +0900, Paul E.C. Melis [paul@floorball.nl](mailto:paul@floorball.nl) wrote:
> 
> > The defined initialize method in the extension actually does nothing, so I  
> > would rather leave it out. I don’t see any reason myself why object  
> > construction and initialization can’t be done completely by the C functions  
> > implementing new and new\_from\_file. Is there anything special about the  
> > “new” singleton method other than that it implicitly calls “initialize”?
> 
> You would be surprised when you create a subclass.
> 
> –  
> Nobu Nakada

---

<div class="post-metadata">

**Author:** ![Gavin\_Sinclair](https://avatars.discourse-cdn.com/v4/letter/g/db5fbb/32.png) [@Gavin\_Sinclair](https://rubytalk.org/u/Gavin_Sinclair)\
**Post date:** [17 October 2002 12:23 UTC](https://rubytalk.org/t/multiple-constructors-in-a-c-extension/2343/4 "2002-10-17T12:23:53Z")

</div>

> Paul E.C. Melis wrote:
> 
> This brings another question, why does ruby use as default an instance  
> method (initialize) for doing what must be a class method (new). I think  
> that the current approach, does not follow the least surprise principle.

When you perform “x = Froz.new”, you’re not calling Froz.new, you’re callnig  
Class.new, because Froz.is\_a? Class.

Class.new performs some groundwork for creating an object, and then hands over  
to Froz.initialize. :initialize does exactly what its name says: it  
initialises the object. It couldn’t be a class method: it needs to access  
instance data.

It may be restrictive, or not, but you do need some protocol.

> You can of course do:
> 
> class Test  
> def Test.new\_from\_file(‘someFile’)  
> aTempVariable=Test.new # because it seems to be the only  
> contructor allowed  
> # … whatever other things this routine does  
> return aTempVariable  
> end  
> end
> 
> This way you can then
> 
> aTestVar = Test.new\_from\_file(‘someFile’)
> 
> But it’s unnatural and strange (at least for me …)

Not for me. Just rename to method to from\_file, and the client code reads

test\_var = Test.from\_file(‘some\_file’)

Not too bad, is it? I would go for Test.new(‘file’) (I’d probably use a File  
object, actually). Anyway, there’s plenty of precendent:

Time.now  
Time.local  
Thread.current  
…

> Enric

Gavin

> **···**
>
> From: “Enric Lafont” [enric@1smart.com](mailto:enric@1smart.com)

---

<div class="post-metadata">

**Author:** ![William\_Djaja\_Tjokr1](https://avatars.discourse-cdn.com/v4/letter/w/87869e/32.png) [@William\_Djaja\_Tjokr1](https://rubytalk.org/u/William_Djaja_Tjokr1)\
**Post date:** [17 October 2002 14:27 UTC](https://rubytalk.org/t/multiple-constructors-in-a-c-extension/2343/5 "2002-10-17T14:27:14Z")

</div>

Hi,

From the “Programming Ruby” book:

"… Data\_Make\_Struct. However, although this allocates memory for the  
object, it does not automatically call an initialize method; you need to  
do that yourself:

…  
rb\_obj\_call\_init(…

This has the benefit of allowing subclasses in Ruby to override or augment  
the basic initialize in your class. …"

If you just write everything in C, then it will be fine. However, when a  
subclass is created in Ruby (not in C), then you will see the  
consequence. I think the best way to understand this is by  
experimentation by, as Nobu pointed out, having a base class and a derived  
class.

Regards,

Bill

> **···**
>
> ==========================================  
> [nobu.nokada@softhome.net](mailto:nobu.nokada@softhome.net) wrote:
> 
> > Hi,
> 
> > At Thu, 17 Oct 2002 19:46:45 +0900, \> Paul E.C. Melis [paul@floorball.nl](mailto:paul@floorball.nl) wrote:
> > 
> > > The defined initialize method in the extension actually does nothing, so I  
> > > would rather leave it out. I don’t see any reason myself why object  
> > > construction and initialization can’t be done completely by the C functions  
> > > implementing new and new\_from\_file. Is there anything special about the  
> > > “new” singleton method other than that it implicitly calls “initialize”?
> 
> > You would be surprised when you create a subclass.
> 
> > –  
> > Nobu Nakada

---

<div class="post-metadata">

**Author:** ![Enric\_Lafont](https://avatars.discourse-cdn.com/v4/letter/e/e8c25b/32.png) [@Enric\_Lafont](https://rubytalk.org/u/Enric_Lafont)\
**Post date:** [17 October 2002 13:12 UTC](https://rubytalk.org/t/multiple-constructors-in-a-c-extension/2343/6 "2002-10-17T13:12:07Z")

</div>

Gavin Sinclair wrote:

> Not for me. Just rename to method to from\_file, and the client code reads
> 
> test\_var = Test.from\_file(‘some\_file’)
> 
> Not too bad, is it? I would go for Test.new(‘file’) (I’d probably use a File  
> object, actually). Anyway, there’s plenty of precendent:
> 
> Time.now  
> Time.local  
> Thread.current  
> …

Sorry to ask, I’m a newbie with Ruby, and I don’t understand what means

“Just rename to method to from\_file”

What method must I rename ? And how to do it ? I’ve done a search on the  
ruby/lib sources and I can not find where Time.now is defined.

Thanks

Enric

---

<div class="post-metadata">

**Author:** ![William\_Djaja\_Tjokr1](https://avatars.discourse-cdn.com/v4/letter/w/87869e/32.png) [@William\_Djaja\_Tjokr1](https://rubytalk.org/u/William_Djaja_Tjokr1)\
**Post date:** [17 October 2002 14:07 UTC](https://rubytalk.org/t/multiple-constructors-in-a-c-extension/2343/7 "2002-10-17T14:07:10Z")

</div>

Hi Enric,

I think what Gavin meant was to replace “Test.new\_from\_file” with  
“Test.from\_file” using a text editor.

There is an online version of the “Programming Ruby” book (which is great  
for tutorial and reference, especially for beginners) at  
[http://www.rubycentral.com/book/](http://www.rubycentral.com/book/). One of the chapters is “Built-in  
Classes and Methods”, and in there you will find the classes Time and  
Thread with all their methods.

Regards,

Bill

> **···**
>
> ===========================================================================  
> Enric Lafont [enric@1smart.com](mailto:enric@1smart.com) wrote:
> 
> > Gavin Sinclair wrote:
> 
> > > Not for me. Just rename to method to from\_file, and the client code reads
> > > 
> > > test\_var = Test.from\_file(‘some\_file’)
> > > 
> > > Not too bad, is it? I would go for Test.new(‘file’) (I’d probably use a File  
> > > object, actually). Anyway, there’s plenty of precendent:
> > > 
> > > Time.now  
> > > Time.local  
> > > Thread.current  
> > > …
> > 
> > Sorry to ask, I’m a newbie with Ruby, and I don’t understand what means
> 
> > “Just rename to method to from\_file”
> 
> > What method must I rename ? And how to do it ? I’ve done a search on the  
> > ruby/lib sources and I can not find where Time.now is defined.
> 
> > Thanks
> 
> > Enric
