[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

preventing instantiation

R. Mark Volkmann

6/19/2005 5:59:00 PM

What is the recommended way in Ruby to prevent other classes from creating
instances of a given class? The class isn't a singleton. It creates several
instances of itself and no additional instances should be able to be created.

I considered making the new method private, but thought there might be a better
way.

--
R. Mark Volkmann
Partner, Object Computing, Inc.


12 Answers

Gavri Fernandez

6/19/2005 6:27:00 PM

0

On 6/19/05, R. Mark Volkmann <mark@ociweb.com> wrote:
> What is the recommended way in Ruby to prevent other classes from creating
> instances of a given class? The class isn't a singleton. It creates several
> instances of itself and no additional instances should be able to be created.
>
> I considered making the new method private, but thought there might be a better
> way.

What could be a better way than making new private? :)
The simplest solution is the best!

--
gavri
http://livejournal.com/us...


Assaph Mehr

6/19/2005 11:40:00 PM

0


>> I considered making the new method private, but thought there might be
>> a better way.
>
> What could be a better way than making new private? :)
> The simplest solution is the best!

In addition, look at the multiton library:
http://raa.ruby-lang.org/project...

HTH,
Assaph

R. Mark Volkmann

6/20/2005 11:08:00 AM

0

Quoting Gavri Fernandez <gavri.fernandez@gmail.com>:

> On 6/19/05, R. Mark Volkmann <mark@ociweb.com> wrote:
> > What is the recommended way in Ruby to prevent other classes from creating
> > instances of a given class? The class isn't a singleton. It creates
> several
> > instances of itself and no additional instances should be able to be
> created.
> >
> > I considered making the new method private, but thought there might be a
> better
> > way.
>
> What could be a better way than making new private? :)
> The simplest solution is the best!

I thought I knew how to do this, but maybe not. These doesn't work.

private :new

private :MyClass.new

--
R. Mark Volkmann
Partner, Object Computing, Inc.


R. Mark Volkmann

6/20/2005 1:25:00 PM

0

Quoting Gavri Fernandez <gavri.fernandez@gmail.com>:

> On 6/19/05, R. Mark Volkmann <mark@ociweb.com> wrote:
> > What is the recommended way in Ruby to prevent other classes from creating
> > instances of a given class? The class isn't a singleton. It creates
> several
> > instances of itself and no additional instances should be able to be
> created.
> >
> > I considered making the new method private, but thought there might be a
> better
> > way.
>
> What could be a better way than making new private? :)
> The simplest solution is the best!

When I put the following in my class, it seems that even that class can't create
instances.

private_class_method :new

I'm confused. How can I create a few objects within the class and then prevent
other classes from creating additional instances?

--
R. Mark Volkmann
Partner, Object Computing, Inc.


Devin Mullins

6/20/2005 2:04:00 PM

0

Why do you need to strictly enforce instantiation of your objects? Are
you not in a position to implement dependency injection, and let the
container manage instantiation? If not...

R. Mark Volkmann wrote:

>I'm confused. How can I create a few objects within the class and then prevent
>other classes from creating additional instances?
>
>
class Moo
def initialize
#initialize is an instance method that gets called right after
construction
#by the actual constructor, a predefined class method called 'new'
puts 'created!'
end
end

a = Moo.new
created!
=> #<Moo:0x2acc368>

def make_singleton(aClass)
#use this to define class methods (like new)
class << aClass
def inst
@inst ||= new #method calls don't need parens
end
#meh, i decided I want the existing 'new' method to be private
private :new
end
end

make_singleton(Moo)

b = Moo.new
NoMethodError: private method `new' called for Moo:Class
from (irb):21

b = Moo.inst
created!
=> #<Moo:0x2ab4020>

c = Moo.inst
=> #<Moo:0x2ab4020>



Steven Jenkins

6/20/2005 2:12:00 PM

0

R. Mark Volkmann wrote:
> Quoting Gavri Fernandez <gavri.fernandez@gmail.com>:
>
>
>>On 6/19/05, R. Mark Volkmann <mark@ociweb.com> wrote:
>>
>>>What is the recommended way in Ruby to prevent other classes from creating
>>>instances of a given class? The class isn't a singleton. It creates
>>
>>several
>>
>>>instances of itself and no additional instances should be able to be
>>
>>created.
>>
>>>I considered making the new method private, but thought there might be a
>>
>>better
>>
>>>way.
>>
>>What could be a better way than making new private? :)
>>The simplest solution is the best!
>
>
> When I put the following in my class, it seems that even that class can't create
> instances.
>
> private_class_method :new
>
> I'm confused. How can I create a few objects within the class and then prevent
> other classes from creating additional instances?

Does this do it?

sjenkins@tidal ~ $ cat t.rb
require 'singleton'

class A; end

a0 = A.new
a1 = A.new
a2 = A.new

class A
include Singleton
end

a3 = A.new
sjenkins@tidal ~ $ ruby t.rb
t.rb:13: private method `new' called for A:Class (NoMethodError)

Remember, 'include' is exectuable.

Steve


R. Mark Volkmann

6/20/2005 2:19:00 PM

0

Quoting Steven Jenkins <steven.jenkins@ieee.org>:

> >>On 6/19/05, R. Mark Volkmann <mark@ociweb.com> wrote:
> >>
> >>>What is the recommended way in Ruby to prevent other classes from creating
> >>>instances of a given class? The class isn't a singleton. It creates
> >>>several
> >>>instances of itself and no additional instances should be able to be
> >>>created.
> >
> > When I put the following in my class, it seems that even that class can't
> > create instances.
> >
> > private_class_method :new
> >
> > I'm confused. How can I create a few objects within the class and then
> > prevent other classes from creating additional instances?
>
> Does this do it?
>
> sjenkins@tidal ~ $ cat t.rb
> require 'singleton'
>
> class A; end
>
> a0 = A.new
> a1 = A.new
> a2 = A.new
>
> class A
> include Singleton
> end
>
> a3 = A.new
> sjenkins@tidal ~ $ ruby t.rb
> t.rb:13: private method `new' called for A:Class (NoMethodError)
>
> Remember, 'include' is exectuable.

That does work, but I see two issues with that approach.
1) it adds an "instance" method that isn't useful and is misleading
2) someone might think the class is a singleton when in fact it isn't

--
R. Mark Volkmann
Partner, Object Computing, Inc.


R. Mark Volkmann

6/20/2005 2:24:00 PM

0

> On 6/19/05, R. Mark Volkmann <mark@ociweb.com> wrote:
>
> What is the recommended way in Ruby to prevent other classes from
> creating
> instances of a given class? The class isn't a singleton. It creates
> several
> instances of itself and no additional instances should be able to be
> created.
>
> When I put the following in my class, it seems that even that class can't
> create instances.
>
> private_class_method :new
>
> I'm confused. How can I create a few objects within the class and then
> prevent other classes from creating additional instances?

I think I've found the solution I was looking for. I can use

private_class_method :new

I just have to put that line AFTER I create all the instances I want to allow.

--
R. Mark Volkmann
Partner, Object Computing, Inc.


Steven Jenkins

6/20/2005 3:09:00 PM

0

R. Mark Volkmann wrote:
> That does work, but I see two issues with that approach.
> 1) it adds an "instance" method that isn't useful and is misleading

You can get rid of it, just as you're getting rid of the "new" method
you no longer want.

> 2) someone might think the class is a singleton when in fact it isn't

Ruby has the '#' construct for just this occasion :-)

But I agree, it's not pretty. You advanced quickly from needing *a* way
to finding some ways wanting. :-)

Steve



Gennady

6/20/2005 3:13:00 PM

0

R. Mark Volkmann wrote:

>
> When I put the following in my class, it seems that even that class can't create
> instances.
>
> private_class_method :new
>
> I'm confused. How can I create a few objects within the class and then prevent
> other classes from creating additional instances?
>
> --
> R. Mark Volkmann
> Partner, Object Computing, Inc.
>

When you declare :new private with private_class_method, the class
itself CAN call method "new", as long as it is invoked without self:

class Test
def self.create(*args)
self.new(*args) # does not work
new(*args) # works !!!
end
end

This is because by definition, you cannot specify a receiver when
calling private methods.

Hope this helps,
Gennady.