Making 'new' private in a C extension

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!

Alle giovedì 27 settembre 2007, [email protected] 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

On Fri, 2007-09-28 at 02:55 +0900, [email protected] 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. :slight_smile:

On Fri, Sep 28, 2007 at 02:55:12AM +0900, [email protected]
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

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”);

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs