Protected class methods

Discussion in 'Ruby' started by listrecv@gmail.com, May 29, 2006.

  1. Guest

    How can I declare a class method protected?
     
    , May 29, 2006
    #1
    1. Advertising

  2. gcarst Guest

    gcarst, May 29, 2006
    #2
    1. Advertising

  3. Guest

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

    I can do:
    private_class_method compressable

    but not protected_class_method
     
    , May 29, 2006
    #3
  4. Guest

    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.)
     
    , May 29, 2006
    #4
  5. Guest

    Is this doable? Yes, no?
     
    , May 29, 2006
    #5
  6. Slasher85 Guest

    def method2

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

    protected :method2
     
    Slasher85, May 30, 2006
    #6
  7. zimbatm Guest

    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
     
    zimbatm, May 30, 2006
    #7
  8. Dave Burt Guest

    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
     
    Dave Burt, May 30, 2006
    #8
  9. Guest

    > 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.
     
    , May 30, 2006
    #9
  10. Dave Burt Guest

    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
     
    Dave Burt, May 31, 2006
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andreas Klemt
    Replies:
    2
    Views:
    565
    Andreas Klemt
    Jul 5, 2003
  2. Tigera
    Replies:
    2
    Views:
    692
    Archie
    May 29, 2007
  3. ankur
    Replies:
    16
    Views:
    1,191
    ankur
    Nov 23, 2007
  4. Gioele Barabucci

    Protected methods and class methods

    Gioele Barabucci, Jan 20, 2006, in forum: Ruby
    Replies:
    3
    Views:
    198
    Ross Bamford
    Jan 22, 2006
  5. Kenneth McDonald
    Replies:
    5
    Views:
    329
    Kenneth McDonald
    Sep 26, 2008
Loading...

Share This Page