# Making 'new' private in a C extension

**URL:** <https://rubytalk.org/t/making-new-private-in-a-c-extension/40977>\
**Category:** ruby-talk\
**Created:** [27 September 2007 17:55 UTC](https://rubytalk.org/t/making-new-private-in-a-c-extension/40977 "2007-09-27T17:55:12Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alterego](https://avatars.discourse-cdn.com/v4/letter/a/6bbea6/32.png) [@Alterego](https://rubytalk.org/u/Alterego)\
**Post date:** [27 September 2007 17:55 UTC](https://rubytalk.org/t/making-new-private-in-a-c-extension/40977/1 "2007-09-27T17:55:12Z")

</div>

Hello all,

&nbsp;&nbsp;I'm writing a C extension, one of the classes I define can only be  
instantiated via a defined 'get\_instance' singleton method. This method will  
only ever return one instance, that is, successive calls will always return  
exactly the same ruby object.

&nbsp;&nbsp;My problem is that someone could still call 'new' on the class. I'd like to  
remove that possibility by making 'new' private. Unfortunately I can't for the  
life of me work out how.

&nbsp;&nbsp;Any help on this subject will be much appreciated. - Tom

> **···**
>
> # ============= &nbsp;&nbsp;Go Burma!

---

<div class="post-metadata">

**Author:** ![Stefano\_Crocco](https://avatars.discourse-cdn.com/v4/letter/s/5f9b8f/32.png) [@Stefano\_Crocco](https://rubytalk.org/u/Stefano_Crocco)\
**Post date:** [28 September 2007 07:21 UTC](https://rubytalk.org/t/making-new-private-in-a-c-extension/40977/2 "2007-09-28T07:21:49Z")

</div>

Alle giovedì 27 settembre 2007, alterego@sdf.lonestar.org ha scritto:

> Hello all,
> 
> &nbsp;&nbsp;I'm writing a C extension, one of the classes I define can only be  
> instantiated via a defined 'get\_instance' singleton method. This method  
> will only ever return one instance, that is, successive calls will always  
> return exactly the same ruby object.
> 
> &nbsp;&nbsp;My problem is that someone could still call 'new' on the class. I'd like  
> to remove that possibility by making 'new' private. Unfortunately I can't  
> for the life of me work out how.
> 
> &nbsp;&nbsp;Any help on this subject will be much appreciated. - Tom
> 
> # ============= &nbsp;&nbsp;Go Burma!

I think you need to use the C function rb\_mod\_private\_method, defined in  
eval.c, which corresponds to the ruby method Module.private\_class\_method. But  
I think you can use the Singleton module provided with ruby: include it in  
your class and you get a class of which only an instance can exist. From the  
C code, you can use the function rb\_mod\_include.

I hope this helps

Stefano

---

<div class="post-metadata">

**Author:** ![Arlen\_Christian\_Mar1](https://avatars.discourse-cdn.com/v4/letter/a/9de053/32.png) [@Arlen\_Christian\_Mar1](https://rubytalk.org/u/Arlen_Christian_Mar1)\
**Post date:** [28 September 2007 12:05 UTC](https://rubytalk.org/t/making-new-private-in-a-c-extension/40977/3 "2007-09-28T12:05:41Z")

</div>

If it helps, you'd just do a "private :new" in the class -- see the  
other guy's response for what looks to be correct. I haven't written a C  
extension though, so I don't know how you'd do the equivalent.

Good luck. 🙂

> **···**
>
> On Fri, 2007-09-28 at 02:55 +0900, alterego@sdf.lonestar.org wrote:
> 
> > Hello all,
> > 
> > &nbsp;&nbsp;I'm writing a C extension, one of the classes I define can only be  
> > instantiated via a defined 'get\_instance' singleton method. This method will  
> > only ever return one instance, that is, successive calls will always return  
> > exactly the same ruby object.
> > 
> > &nbsp;&nbsp;My problem is that someone could still call 'new' on the class. I'd like to  
> > remove that possibility by making 'new' private. Unfortunately I can't for the  
> > life of me work out how.
> > 
> > &nbsp;&nbsp;Any help on this subject will be much appreciated. - Tom

---

<div class="post-metadata">

**Author:** ![Paul\_Brannan](https://avatars.discourse-cdn.com/v4/letter/p/76d3ee/32.png) [@Paul\_Brannan](https://rubytalk.org/u/Paul_Brannan)\
**Post date:** [28 September 2007 13:23 UTC](https://rubytalk.org/t/making-new-private-in-a-c-extension/40977/4 "2007-09-28T13:23:28Z")

</div>

> Hello all,
> 
> &nbsp;&nbsp;I'm writing a C extension, one of the classes I define can only be  
> instantiated via a defined 'get\_instance' singleton method. This method will  
> only ever return one instance, that is, successive calls will always return  
> exactly the same ruby object.

In Ruby, such a method is generally named 'instance' rather than  
'get\_instance'.

> &nbsp;&nbsp;My problem is that someone could still call 'new' on the class. I'd like to  
> remove that possibility by making 'new' private. Unfortunately I can't for the  
> life of me work out how.

&nbsp;&nbsp;VALUE rb\_cFoo = rb\_define\_class("Foo", rb\_cObject);  
&nbsp;&nbsp;rb\_undef\_alloc\_func(rb\_cFoo);

Paul

> **···**
>
> On Fri, Sep 28, 2007 at 02:55:12AM +0900, alterego@sdf.lonestar.org wrote:

---

<div class="post-metadata">

**Author:** ![Nobuyoshi\_Nakada1](https://avatars.discourse-cdn.com/v4/letter/n/9f8e36/32.png) [@Nobuyoshi\_Nakada1](https://rubytalk.org/u/Nobuyoshi_Nakada1)\
**Post date:** [28 September 2007 18:39 UTC](https://rubytalk.org/t/making-new-private-in-a-c-extension/40977/5 "2007-09-28T18:39:05Z")

</div>

Hi,

At Fri, 28 Sep 2007 22:23:28 +0900,  
Paul Brannan wrote in [ruby-talk:271468]:

> \> My problem is that someone could still call 'new' on the class. I'd like to  
> \> remove that possibility by making 'new' private. Unfortunately I can't for the  
> \> life of me work out how.
> 
> &nbsp;&nbsp;VALUE rb\_cFoo = rb\_define\_class("Foo", rb\_cObject);  
> &nbsp;&nbsp;rb\_undef\_alloc\_func(rb\_cFoo);

It makes impossible to create any instances at all.

&nbsp;&nbsp;rb\_undef\_method(rb\_singleton\_class(rb\_cFoo), "new");

> **···**
>
> --  
> Nobu Nakada

---

<div class="post-metadata">

**Author:** ![Paul\_Brannan](https://avatars.discourse-cdn.com/v4/letter/p/76d3ee/32.png) [@Paul\_Brannan](https://rubytalk.org/u/Paul_Brannan)\
**Post date:** [5 October 2007 13:57 UTC](https://rubytalk.org/t/making-new-private-in-a-c-extension/40977/6 "2007-10-05T13:57:10Z")

</div>

Instances can still be created with Data\_Wrap\_Struct. But maybe that's  
not what the OP intended. I saw the word "extension" and made an  
assumption.

Paul

> **···**
>
> On Sat, Sep 29, 2007 at 03:39:05AM +0900, Nobuyoshi Nakada wrote:
> 
> > \> VALUE rb\_cFoo = rb\_define\_class("Foo", rb\_cObject);  
> > \> rb\_undef\_alloc\_func(rb\_cFoo);
> > 
> > It makes impossible to create any instances at all.
> > 
> > &nbsp;&nbsp;rb\_undef\_method(rb\_singleton\_class(rb\_cFoo), "new");
