Making 'new' private in a C extension.

A

alterego

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

Stefano Crocco

Alle gioved=EC 27 settembre 2007, (e-mail address removed) 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

=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
Go Burma!
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D

I think you need to use the C function rb_mod_private_method, defined in=20
eval.c, which corresponds to the ruby method Module.private_class_method. B=
ut=20
I think you can use the Singleton module provided with ruby: include it in=
=20
your class and you get a class of which only an instance can exist. From th=
e=20
C code, you can use the function rb_mod_include.

I hope this helps

Stefano
 
P

Paul Brannan

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
 
N

Nobuyoshi Nakada

Hi,

At Fri, 28 Sep 2007 22:23:28 +0900,
Paul Brannan wrote in [ruby-talk:271468]:
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");
 
P

Paul Brannan

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top