# Private method calls...why can't you use self.priv\_method syntax?

**URL:** <https://www.ruby-forum.com/t/private-method-calls-why-cant-you-use-self-priv-method-syntax/169478>\
**Category:** Ruby\
**Created:** [June 4, 2009, 8:55am UTC](https://www.ruby-forum.com/t/private-method-calls-why-cant-you-use-self-priv-method-syntax/169478 "2009-06-04T08:55:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![timr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/3e96dc/32.png) [@timr](https://www.ruby-forum.com/u/timr)\
**Post date:** [June 4, 2009, 8:55am UTC](https://www.ruby-forum.com/t/private-method-calls-why-cant-you-use-self-priv-method-syntax/169478/1 "2009-06-04T08:55:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![timr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/3e96dc/32.png) [@timr](https://www.ruby-forum.com/u/timr)\
**Post date:** [June 4, 2009, 9:36am UTC](https://www.ruby-forum.com/t/private-method-calls-why-cant-you-use-self-priv-method-syntax/169478/2 "2009-06-04T09:36:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![timr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/3e96dc/32.png) [@timr](https://www.ruby-forum.com/u/timr)\
**Post date:** [June 4, 2009, 7:16pm UTC](https://www.ruby-forum.com/t/private-method-calls-why-cant-you-use-self-priv-method-syntax/169478/3 "2009-06-04T19:16:16Z")

</div>

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
