private method calls...why can't you use self.priv_method syntax?

Discussion in 'Ruby' started by timr, Jun 4, 2009.

  1. timr

    timr Guest

    Why is it that when you call a private method, you cannot use
    self.the_priv_method?
    Below is an example that works if you take out self before the method,
    but fails with it in.
    It is odd because using self should just a more explicit statement.
    But either way the method is being sent to the same object.
    WEIRD!!!!
    Please let me know how you gurus wrap your heads around this
    (apparent) inconsistency.
    Thanks,
    Tim


    class TestPrivate
    def giveValue
    @value
    end
    def callPriv
    self.priv #why can't you put the self in front of a private method?
    end

    private
    def priv
    @value = 100
    end
    end


    i = TestPrivate.new
    p i
    i.callPriv
    p i.giveValue
    # ~> -:6:in `callPriv': private method `priv' called for #<TestPrivate:
    0x33e1f4> (NoMethodError)
    # ~> from -:18
    # >> #<TestPrivate:0x33e1f4>
    timr, Jun 4, 2009
    #1
    1. Advertising

  2. In Ruby, private means that the method cannot be called with an
    *explicit* receiver (that includes "self"). This is a bit different
    then languages like C++ and Java which use private/protected to
    control calling in inheritance chains. Remember, Ruby is all about the
    callers and receivers. If you leave off the "self." at the front, then
    self is *implicit* (which is allowed for private methods. Try this:

    class MyClass
    def call_protected_on(obj)
    obj.protected_method
    end

    def call_private_on(obj)
    obj.private_method
    end

    protected
    def protected_method
    puts "Explicit callers are ok"
    end

    private
    def private_method
    puts "Implicit callers only!"
    end
    end

    my_obj = MyClass.new
    my_other_obj = MyClass.new
    my_obj.call_protected_on(my_other_obj)
    my_obj.call_private_on(my_other_obj)

    Cheers,

    Josh


    On Jun 3, 2009, at 11:55 PM, timr wrote:

    > Why is it that when you call a private method, you cannot use
    > self.the_priv_method?
    > Below is an example that works if you take out self before the method,
    > but fails with it in.
    > It is odd because using self should just a more explicit statement.
    > But either way the method is being sent to the same object.
    > WEIRD!!!!
    > Please let me know how you gurus wrap your heads around this
    > (apparent) inconsistency.
    > Thanks,
    > Tim
    >
    >
    > class TestPrivate
    > def giveValue
    > @value
    > end
    > def callPriv
    > self.priv #why can't you put the self in front of a private method?
    > end
    >
    > private
    > def priv
    > @value = 100
    > end
    > end
    >
    >
    > i = TestPrivate.new
    > p i
    > i.callPriv
    > p i.giveValue
    > # ~> -:6:in `callPriv': private method `priv' called for
    > #<TestPrivate:
    > 0x33e1f4> (NoMethodError)
    > # ~> from -:18
    > # >> #<TestPrivate:0x33e1f4>
    >
    Joshua Ballanco, Jun 4, 2009
    #2
    1. Advertising

  3. timr

    timr Guest

    Josh, thanks for the clarification. I guess I just have to accept the
    syntax for the private method calls. But it is hard for me to
    understand why calling a private method on its own--which has to look
    up self to use as a receiver, is any different than me just giving it
    the receiver of self. Either way the same object is being used as the
    receiver. So there must be something strange about the way private is
    implemented that makes it not ever take receivers, even if you give it
    the receiver that it is going to lookup and use. Okay, I can tell I am
    just grumbling about something that I should just accept. Sorry.
    Thanks again,
    Tim

    On Jun 4, 12:35 am, Joshua Ballanco <> wrote:
    > In Ruby, private means that the method cannot be called with an  
    > *explicit* receiver (that includes "self"). This is a bit different  
    > then languages like C++ and Java which use private/protected to  
    > control calling in inheritance chains. Remember, Ruby is all about the  
    > callers and receivers. If you leave off the "self." at the front, then  
    > self is *implicit* (which is allowed for private methods.
    timr, Jun 4, 2009
    #3
    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. Ralf W. Grosse-Kunstleve
    Replies:
    16
    Views:
    580
    Lonnie Princehouse
    Jul 11, 2005
  2. Ralf W. Grosse-Kunstleve
    Replies:
    18
    Views:
    595
    Bengt Richter
    Jul 11, 2005
  3. Ralf W. Grosse-Kunstleve
    Replies:
    2
    Views:
    399
    Dan Sommers
    Jul 12, 2005
  4. falcon
    Replies:
    0
    Views:
    374
    falcon
    Jul 31, 2005
  5. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,850
    Smokey Grindel
    Dec 2, 2006
Loading...

Share This Page