[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Protected class methods

List Recv

5/28/2006 11:10:00 PM

How can I declare a class method protected?

9 Answers

gcarst

5/29/2006 7:31:00 AM

0

Den Sun, 28 May 2006 16:10:09 -0700. skrev listrecv:

> How can I declare a class method protected?

http://www.rubycentral.com/book/tut_cl...
under access control.

List Recv

5/29/2006 7:35:00 AM

0

Nope. I'm talking about *class* methods.

I can do:
private_class_method compressable

but not protected_class_method

List Recv

5/29/2006 2:26:00 PM

0

I realized that my question maybe incorrect.

What I'd like is a class method which can be called by instances of
that class, but is not public.

(The goal? A class which can only be instantiated by already existing
instances. The reason? To ensure a tree.)

List Recv

5/29/2006 8:35:00 PM

0

Is this doable? Yes, no?

Slasher85

5/30/2006 8:04:00 AM

0

def method2

# ..............

protected :method2

zimba.tm@gmail.com

5/30/2006 8:46:00 AM

0

irb(main):005:0> class Test
irb(main):006:1> class << self
irb(main):007:2> protected
irb(main):008:2> def woot
irb(main):009:3> puts "w00t"
irb(main):010:3> end
irb(main):011:2> end
irb(main):012:1> end
=> nil
irb(main):013:0> class Test
irb(main):014:1> def hey
irb(main):015:2> self.class.woot
irb(main):016:2> end
irb(main):017:1> end
=> nil
irb(main):023:0> t = Test.new
=> #<Test:0xb7c378d4>
irb(main):024:0> t.hey
NoMethodError: protected method `woot' called for Test:Class
from (irb):20:in `hey'
from (irb):24

Dave Burt

5/30/2006 9:41:00 AM

0

listrecv@gmail.com wrote:
> I realized that my question maybe incorrect.
>
> What I'd like is a class method which can be called by instances of
> that class, but is not public.
>
> (The goal? A class which can only be instantiated by already existing
> instances. The reason? To ensure a tree.)

What do you mean by "ensure a tree", or, more generally, what is it
precisely that you're trying to do?

You can rename new, if you think it would help avoid errors (like
Rational in the standard library):

class Foo
alias_method :new! :new
private_class_method :new
end

Cheers,
Dave

List Recv

5/30/2006 2:47:00 PM

0

> What do you mean by "ensure a tree", or, more generally, what is it
precisely that you're trying to do?

I want to ensure that all new instances are created by an already
existing instance, and so incorporated into a tree. No dangling
instances.

vp = pres.new_child # Good

vp = Worker.new # BAD!

def new_child
child = Worker.new
child.parent = self
return child
end

I can't figure out how to mark Worker.new so that it can be called by
instances of Worker but not outside clients.

Dave Burt

5/31/2006 1:56:00 PM

0

listrecv@gmail.com wrote:
>> What do you mean by "ensure a tree", or, more generally, what is it
>> precisely that you're trying to do?
>
> I want to ensure that all new instances are created by an already
> existing instance, and so incorporated into a tree. No dangling
> instances.
>
> vp = pres.new_child # Good
>
> vp = Worker.new # BAD!
>
> def new_child
> child = Worker.new
> child.parent = self
> return child
> end
>
> I can't figure out how to mark Worker.new so that it can be called by
> instances of Worker but not outside clients.

That's logically impossible, given that the initial state of the
interpreter contains no instances of Worker. If Workers can only be
created by instances of Worker, and there are initially no instances of
Worker, there is no way to create any Worker.

Why is renaming the new method not enough?

Cheers,
Dave