# Private method clarification, please

**URL:** <https://www.ruby-forum.com/t/private-method-clarification-please/108460>\
**Category:** Ruby\
**Created:** [July 3, 2007, 8:04pm UTC](https://www.ruby-forum.com/t/private-method-clarification-please/108460 "2007-07-03T20:04:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![timperrett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c5a1d2/32.png) [@timperrett](https://www.ruby-forum.com/u/timperrett)\
**Post date:** [July 3, 2007, 8:04pm UTC](https://www.ruby-forum.com/t/private-method-clarification-please/108460/1 "2007-07-03T20:04:06Z")

</div>

Is this behavior expected?

$ cat t.rb  
class C  
def meth1() private\_method end  
def meth2() self.private\_method end  
def private\_method() puts ‘you called?’ end  
private :private\_method  
end

c = C.new  
c.meth1  
c.meth2

$ ruby --version  
ruby 1.8.5 (2006-12-25 patchlevel 12) [i386-cygwin]

$ ruby t.rb  
you called?  
t.rb:3:in `meth2': private method`private\_method’ called for  
#\<C:0x442fdd4\> (NoMethodError)  
from t.rb:10

It seems distinctly strange to me that you cannot call a private  
method using the self semantics shown in meth2. Is this the expected  
behavior?

Blessings,  
TwP

---

<div class="post-metadata">

**Author:** ![timperrett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c5a1d2/32.png) [@timperrett](https://www.ruby-forum.com/u/timperrett)\
**Post date:** [July 3, 2007, 8:09pm UTC](https://www.ruby-forum.com/t/private-method-clarification-please/108460/2 "2007-07-03T20:09:07Z")

</div>

On 7/3/07, Tim P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> #\<C:0x442fdd4\> (NoMethodError)  
> from t.rb:10
> 
> It seems distinctly strange to me that you cannot call a private  
> method using the self semantics shown in meth2. Is this the expected  
> behavior?

Hmm… I can’t decide is it’s strange or not. I always think of using  
self.whatever in methods as using ‘external’ access, in which case,  
it’s not surprising. You can of course, send() through.

So yeah, I can see where that behaviour might seem surprising but it’s  
not particularly surprising to me.

-greg

---

<div class="post-metadata">

**Author:** ![timperrett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c5a1d2/32.png) [@timperrett](https://www.ruby-forum.com/u/timperrett)\
**Post date:** [July 3, 2007, 8:20pm UTC](https://www.ruby-forum.com/t/private-method-clarification-please/108460/3 "2007-07-03T20:20:11Z")

</div>

On 7/3/07, Gregory B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > end  
> > t.rb:3:in `meth2': private method `private\_method’ called for  
> > it’s not surprising. You can of course, send() through.
> 
> So yeah, I can see where that behaviour might seem surprising but it’s  
> not particularly surprising to me.

The only reason it seems strange is that you can use the self  
semantics in some cases – i.e. when calling a setter method

class C  
def meth3( val ) self.private\_setter = val end  
def private\_setter=( val ) puts “you said ‘#{val}’” end  
private :private\_setter=  
end

c = C.new  
c.meth3( ‘hello’ ) #=\> you said ‘hello’

So, that case works, but others do not. Just seems strange.

TwP

---

<div class="post-metadata">

**Author:** ![timperrett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c5a1d2/32.png) [@timperrett](https://www.ruby-forum.com/u/timperrett)\
**Post date:** [July 3, 2007, 8:35pm UTC](https://www.ruby-forum.com/t/private-method-clarification-please/108460/4 "2007-07-03T20:35:13Z")

</div>

> The only reason it seems strange is that you can use the self  
> semantics in some cases – i.e. when calling a setter method

From what I understand, the difference between private and protected  
methods is that protected methods can only be called by objects of the  
same class, while private methods cannot be called with an explicit  
receiver. This implies that private methods can only be called from  
within the object itself. However, you have to use an explicit receiver  
with setter methods from within the class, otherwise the interpreter  
will think you’re trying to set a local variable. If you couldn’t use  
self with private setter methods there would be no way to call them.  
You could try “private\_setter=(args)” but that might still be parsed as  
an assignment to the local variable private\_setter. It seems a little  
inconsistent but I think it makes sense.

-Brett
