Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Protected class methods

Reply
Thread Tools

Protected class methods

 
 
listrecv@gmail.com
Guest
Posts: n/a
 
      05-28-2006
How can I declare a class method protected?

 
Reply With Quote
 
 
 
 
gcarst
Guest
Posts: n/a
 
      05-29-2006
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_classes.html
under access control.
 
Reply With Quote
 
 
 
 
listrecv@gmail.com
Guest
Posts: n/a
 
      05-29-2006
Nope. I'm talking about *class* methods.

I can do:
private_class_method compressable

but not protected_class_method

 
Reply With Quote
 
listrecv@gmail.com
Guest
Posts: n/a
 
      05-29-2006
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.)

 
Reply With Quote
 
listrecv@gmail.com
Guest
Posts: n/a
 
      05-29-2006
Is this doable? Yes, no?

 
Reply With Quote
 
Slasher85
Guest
Posts: n/a
 
      05-30-2006
def method2

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

protected :method2

 
Reply With Quote
 
zimbatm
Guest
Posts: n/a
 
      05-30-2006
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

 
Reply With Quote
 
Dave Burt
Guest
Posts: n/a
 
      05-30-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) 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
 
Reply With Quote
 
listrecv@gmail.com
Guest
Posts: n/a
 
      05-30-2006
> 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.

 
Reply With Quote
 
Dave Burt
Guest
Posts: n/a
 
      05-31-2006
(E-Mail Removed) 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
calling class methods from class methods, help? Oltmans Python 6 03-11-2009 07:59 PM
Is there a way to find the class methods of a class, just like'methods' finds the instance methods? Kenneth McDonald Ruby 5 09-26-2008 03:09 PM
why a class can't access protected method from another class in thesame package,the method is interited from the ohtner class from differntpackage? junzhang1983@gmail.com Java 3 01-28-2008 02:09 AM
Using protected methods within a derived class? Tigera C++ 2 05-29-2007 05:06 AM
Protected methods and class methods Gioele Barabucci Ruby 3 01-22-2006 07:57 PM



Advertisments