Protected class methods

L

listrecv

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

I can do:
private_class_method compressable

but not protected_class_method
 
L

listrecv

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

zimbatm

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
 
D

Dave Burt

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
 
L

listrecv

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

Dave Burt

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
 

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,206
Messages
2,571,069
Members
47,677
Latest member
MoisesKoeh

Latest Threads

Top