[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Making 'new' private in a C extension.

alterego

9/27/2007 5:55:00 PM


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!
=============

5 Answers

Stefano Crocco

9/28/2007 7:22:00 AM

0

Alle giovedì 27 settembre 2007, alterego@sdf.lonestar.org ha scritto:
> 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!
> =============

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

Arlen Cuss

9/28/2007 12:06:00 PM

0


On Fri, 2007-09-28 at 02:55 +0900, alterego@sdf.lonestar.org 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.
>
> 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


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


Paul Brannan

9/28/2007 1:23:00 PM

0

On Fri, Sep 28, 2007 at 02:55:12AM +0900, alterego@sdf.lonestar.org 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


Nobuyoshi Nakada

9/28/2007 6:39:00 PM

0

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.
>
> 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");

--
Nobu Nakada

Paul Brannan

10/5/2007 1:57:00 PM

0

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