# Making 'new' private in a C extension

**URL:** <https://www.ruby-forum.com/t/making-new-private-in-a-c-extension/115700>\
**Category:** Ruby\
**Created:** [September 27, 2007, 7:55pm UTC](https://www.ruby-forum.com/t/making-new-private-in-a-c-extension/115700 "2007-09-27T19:55:51Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [September 27, 2007, 7:55pm UTC](https://www.ruby-forum.com/t/making-new-private-in-a-c-extension/115700/1 "2007-09-27T19:55:51Z")

</div>

Hello all,

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.

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.

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

# ============= Go Burma!

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [September 28, 2007, 9:22am UTC](https://www.ruby-forum.com/t/making-new-private-in-a-c-extension/115700/2 "2007-09-28T09:22:35Z")

</div>

Alle giovedì 27 settembre 2007, removed\_email\_address@domain.invalid ha scritto:

> Any help on this subject will be much appreciated. - Tom
> 
> # ============= 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:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [September 28, 2007, 2:06pm UTC](https://www.ruby-forum.com/t/making-new-private-in-a-c-extension/115700/3 "2007-09-28T14:06:44Z")

</div>

On Fri, 2007-09-28 at 02:55 +0900, removed\_email\_address@domain.invalid wrote:

> Any help on this subject will be much appreciated. - Tom

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. 🙂

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [September 28, 2007, 3:24pm UTC](https://www.ruby-forum.com/t/making-new-private-in-a-c-extension/115700/4 "2007-09-28T15:24:02Z")

</div>

On Fri, Sep 28, 2007 at 02:55:12AM +0900, removed\_email\_address@domain.invalid  
wrote:

> Hello all,
> 
> 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’.

> 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.

VALUE rb\_cFoo = rb\_define\_class(“Foo”, rb\_cObject);  
rb\_undef\_alloc\_func(rb\_cFoo);

Paul

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [September 28, 2007, 8:40pm UTC](https://www.ruby-forum.com/t/making-new-private-in-a-c-extension/115700/5 "2007-09-28T20:40:19Z")

</div>

Hi,

At Fri, 28 Sep 2007 22:23:28 +0900,  
Paul B. 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.
> 
> VALUE rb\_cFoo = rb\_define\_class(“Foo”, rb\_cObject);  
> rb\_undef\_alloc\_func(rb\_cFoo);

It makes impossible to create any instances at all.

rb\_undef\_method(rb\_singleton\_class(rb\_cFoo), “new”);

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [October 5, 2007, 3:57pm UTC](https://www.ruby-forum.com/t/making-new-private-in-a-c-extension/115700/6 "2007-10-05T15:57:49Z")

</div>

On Sat, Sep 29, 2007 at 03:39:05AM +0900, Nobuyoshi N. 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.
> 
> rb\_undef\_method(rb\_singleton\_class(rb\_cFoo), “new”);

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
