# Private methods and self:

**URL:** <https://www.ruby-forum.com/t/private-methods-and-self/54132>\
**Category:** Ruby\
**Created:** [February 23, 2006, 10:14pm UTC](https://www.ruby-forum.com/t/private-methods-and-self/54132 "2006-02-23T22:14:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [February 23, 2006, 10:14pm UTC](https://www.ruby-forum.com/t/private-methods-and-self/54132/1 "2006-02-23T22:14:58Z")

</div>

FOR ruby 1.8.4 (2005-12-24) [i386-linux]:

#--------------------------------------------------------------------------  
def foo1  
self.bar  
end

def foo2  
bar  
end

def bar  
puts “In bar”  
end

private :bar

if **FILE** == $0

foo2 -\> ‘In bar’  
foo1 -\> ‘NoMethodError: private method `bar’ called for main:Object’

#--------------------------------------------------------------------------

Why the difference in treatment between the explict self reciever and  
the implict self receiver?

Regards,  
Jim

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [February 23, 2006, 11:24pm UTC](https://www.ruby-forum.com/t/private-methods-and-self/54132/2 "2006-02-23T23:24:47Z")

</div>

James B. wrote:

> FOR ruby 1.8.4 (2005-12-24) [i386-linux]:
> 
> #--------------------------------------------------------------------------  
> def foo1  
> self.bar  
> end
> 
> def foo2  
> bar  
> end
> 
> def bar  
> puts “In bar”  
> end
> 
> private :bar
> 
> if **FILE** == $0
> 
> foo2 -\> ‘In bar’  
> foo1 -\> ‘NoMethodError: private method `bar’ called for main:Object’
> 
> #--------------------------------------------------------------------------
> 
> Why the difference in treatment between the explict self reciever and  
> the implict self receiver?

Private methods are not allowed to have an explicit receiver  
(using the ‘self’ there is like going out of the object and  
then sending the message). The only exception to this rule  
are writer methods (def foo=(); …; end) because without  
the self., foo = x is always interpreted as an assignment  
to a local variable.

> Regards,  
> Jim

E

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [February 23, 2006, 11:37pm UTC](https://www.ruby-forum.com/t/private-methods-and-self/54132/3 "2006-02-23T23:37:22Z")

</div>

E. Saynatkari wrote:

> Private methods are not allowed to have an explicit receiver  
> (using the ‘self’ there is like going out of the object and  
> then sending the message). The only exception to this rule  
> are writer methods (def foo=(); …; end) because without  
> the self., foo = x is always interpreted as an assignment  
> to a local variable.
> 
> E

Curious that this restriction does not appear to be mentioned in the  
Pickaxe book. It also seems, to me, somewhat counter-intuitive. Is it  
purposeful behaviour or an artifact of a stylistic convention?

Regards,  
Jim

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [February 24, 2006, 12:20am UTC](https://www.ruby-forum.com/t/private-methods-and-self/54132/4 "2006-02-24T00:20:47Z")

</div>

James B. wrote:

> > E
> 
> Curious that this restriction does not appear to be mentioned in the  
> Pickaxe book. It also seems, to me, somewhat counter-intuitive. Is it  
> purposeful behaviour or an artifact of a stylistic convention?
> 
> Regards,  
> Jim

Page 35 in the 2nd Edition: “Private methods cannot be called with an  
explicit receiverâ??the receiver is always self. This means that private  
methods can be called only in the context of the current object; you  
canâ??t invoke another objectâ??s private methods.”
