# Newbie question : private / public methods

**URL:** <https://rubytalk.org/t/newbie-question-private-public-methods/45322>\
**Category:** ruby-talk\
**Created:** [21 March 2008 14:14 UTC](https://rubytalk.org/t/newbie-question-private-public-methods/45322 "2008-03-21T14:14:40Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Krzysieq](https://avatars.discourse-cdn.com/v4/letter/k/71e660/32.png) [@Krzysieq](https://rubytalk.org/u/Krzysieq)\
**Post date:** [21 March 2008 14:14 UTC](https://rubytalk.org/t/newbie-question-private-public-methods/45322/1 "2008-03-21T14:14:40Z")

</div>

Hi,

I'm new both to this mailing list and to Ruby. Here's an example, question  
will come later.

> > private.rb

class PrivateMethodTest  
&nbsp;&nbsp;&nbsp;&nbsp;private  
&nbsp;&nbsp;&nbsp;&nbsp;def privateMethod(the\_string)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'haha! ' + the\_string  
&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;public  
&nbsp;&nbsp;&nbsp;&nbsp;def publicMethod(the\_string)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.privateMethod(the\_string)  
&nbsp;&nbsp;&nbsp;&nbsp;end  
end

> > private\_test.rb

require 'private.rb'  
require 'test/unit'

class PrivateMethodTestTest \< Test::Unit::TestCase

&nbsp;&nbsp;&nbsp;&nbsp;def test\_publicMethod  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var = PrivateMethodTest.new()  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;assert\_equal('haha! test', var.publicMethod('test'))  
&nbsp;&nbsp;&nbsp;&nbsp;end  
end

Now the question: why is it, that when the self. (bolded in the code) is  
there, the test fails, whereas if I remove it, the test is successful? This  
is the failure message:

&nbsp;&nbsp;1) Error:  
test\_publicMethod(PrivateMethodTestTest):  
NoMethodError: private method `privateMethod' called for  
#\<PrivateMethodTest:0x2b0876d805a0\>  
&nbsp;&nbsp;&nbsp;&nbsp;./private.rb:9:in `publicMethod'  
&nbsp;&nbsp;&nbsp;&nbsp;private\_test.rb:8:in `test\_publicMethod'

What am I missing?

Thanks for any help and sorry if this question is silly 🙂  
Cheers,  
Chris

---

<div class="post-metadata">

**Author:** ![Mateusz\_Tybura](https://avatars.discourse-cdn.com/v4/letter/m/cc9497/32.png) [@Mateusz\_Tybura](https://rubytalk.org/u/Mateusz_Tybura)\
**Post date:** [21 March 2008 14:29 UTC](https://rubytalk.org/t/newbie-question-private-public-methods/45322/2 "2008-03-21T14:29:08Z")

</div>

I've get some other error form this code. Check at:  
[http://img137.imageshack.us/img137/5754/errorxb0.jpg](http://img137.imageshack.us/img137/5754/errorxb0.jpg)

> **···**
>
> 2008/3/21, krzysieq Gazeta.pl \<krzysieq@gazeta.pl\>:
> 
> > Hi,
> > 
> > I'm new both to this mailing list and to Ruby. Here's an example, question  
> > will come later.
> > 
> > \>\> private.rb
> > 
> > class PrivateMethodTest  
> > &nbsp;&nbsp;&nbsp;&nbsp;private  
> > &nbsp;&nbsp;&nbsp;&nbsp;def privateMethod(the\_string)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'haha! ' + the\_string  
> > &nbsp;&nbsp;&nbsp;&nbsp;end
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;public  
> > &nbsp;&nbsp;&nbsp;&nbsp;def publicMethod(the\_string)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.privateMethod(the\_string)  
> > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > end
> > 
> > \>\> private\_test.rb
> > 
> > require 'private.rb'  
> > require 'test/unit'
> > 
> > class PrivateMethodTestTest \< Test::Unit::TestCase
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;def test\_publicMethod  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var = PrivateMethodTest.new()  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;assert\_equal('haha! test', var.publicMethod('test'))  
> > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > end
> > 
> > Now the question: why is it, that when the self. (bolded in the code) is  
> > there, the test fails, whereas if I remove it, the test is successful?  
> > This  
> > is the failure message:
> > 
> > &nbsp;&nbsp;1) Error:  
> > test\_publicMethod(PrivateMethodTestTest):  
> > NoMethodError: private method `privateMethod' called for  
> > #\<PrivateMethodTest:0x2b0876d805a0\>  
> > &nbsp;&nbsp;&nbsp;&nbsp;./private.rb:9:in `publicMethod'  
> > &nbsp;&nbsp;&nbsp;&nbsp;private\_test.rb:8:in `test\_publicMethod'
> > 
> > What am I missing?
> > 
> > Thanks for any help and sorry if this question is silly 🙂  
> > Cheers,  
> > Chris

---

<div class="post-metadata">

**Author:** ![Stefano\_Crocco](https://avatars.discourse-cdn.com/v4/letter/s/5f9b8f/32.png) [@Stefano\_Crocco](https://rubytalk.org/u/Stefano_Crocco)\
**Post date:** [21 March 2008 14:36 UTC](https://rubytalk.org/t/newbie-question-private-public-methods/45322/3 "2008-03-21T14:36:21Z")

</div>

Ruby implements private methods as methods which can't be called using an  
explicit receiver (i.e, using the form obj.method\_name), but only using the  
implicit receiver (that is, method\_name). Since the implicit receiver is  
always self, this ensures that private instance methods can only be called by  
instance methods of that class (or of derived classes) and can't be accessed  
by other instances of the same class. An unpleasant side effect of this is  
that you can't call private methods using the explicit receiver even when that  
receiver is equal to the implicit one (that is, self), which is the situation  
you discovered.

> **···**
>
> On Friday 21 March 2008, krzysieq Gazeta.pl wrote:
> 
> > Hi,
> > 
> > I'm new both to this mailing list and to Ruby. Here's an example, question  
> > will come later.
> > 
> > \>\> private.rb
> > 
> > class PrivateMethodTest  
> > &nbsp;&nbsp;&nbsp;&nbsp;private  
> > &nbsp;&nbsp;&nbsp;&nbsp;def privateMethod(the\_string)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'haha! ' + the\_string  
> > &nbsp;&nbsp;&nbsp;&nbsp;end
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;public  
> > &nbsp;&nbsp;&nbsp;&nbsp;def publicMethod(the\_string)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.privateMethod(the\_string)  
> > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > end
> > 
> > \>\> private\_test.rb
> > 
> > require 'private.rb'  
> > require 'test/unit'
> > 
> > class PrivateMethodTestTest \< Test::Unit::TestCase
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;def test\_publicMethod  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var = PrivateMethodTest.new()  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;assert\_equal('haha! test', var.publicMethod('test'))  
> > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > end
> > 
> > Now the question: why is it, that when the self. (bolded in the code) is  
> > there, the test fails, whereas if I remove it, the test is successful? This  
> > is the failure message:
> > 
> > &nbsp;&nbsp;1) Error:  
> > test\_publicMethod(PrivateMethodTestTest):  
> > NoMethodError: private method `privateMethod' called for  
> > #\<PrivateMethodTest:0x2b0876d805a0\>  
> > &nbsp;&nbsp;&nbsp;&nbsp;./private.rb:9:in `publicMethod'  
> > &nbsp;&nbsp;&nbsp;&nbsp;private\_test.rb:8:in `test\_publicMethod'
> > 
> > What am I missing?
> > 
> > Thanks for any help and sorry if this question is silly 🙂  
> > Cheers,  
> > Chris

---

<div class="post-metadata">

**Author:** ![Krzysieq](https://avatars.discourse-cdn.com/v4/letter/k/71e660/32.png) [@Krzysieq](https://rubytalk.org/u/Krzysieq)\
**Post date:** [21 March 2008 15:02 UTC](https://rubytalk.org/t/newbie-question-private-public-methods/45322/4 "2008-03-21T15:02:50Z")

</div>

Then it enforces habits, which I've been encouraged to develop anyways - my  
managers always told me not to use this.doSomething whenever there is no  
risk of ambiguity. Don't You think however, that this is quite harsh - I  
mean, don't You think this might get someone into bigger trouble sometime? I  
don't know Ruby well enough to think of an example yet, but something tells  
me this "feature" could be tricky...

Mateusz: You changed the name of the test class, that's why the error.

Cheers,  
Chris

> **···**
>
> 2008/3/21, Stefano Crocco \<stefano.crocco@alice.it\>:
> 
> > On Friday 21 March 2008, krzysieq Gazeta.pl wrote:  
> > \> Hi,  
> > \>  
> > \> I'm new both to this mailing list and to Ruby. Here's an example,  
> > question  
> > \> will come later.  
> > \>  
> > \> \>\> private.rb  
> > \>  
> > \> class PrivateMethodTest  
> > \> private  
> > \> def privateMethod(the\_string)  
> > \> 'haha! ' + the\_string  
> > \> end  
> > \>  
> > \> public  
> > \> def publicMethod(the\_string)  
> > \> self.privateMethod(the\_string)  
> > \> end  
> > \> end  
> > \>  
> > \> \>\> private\_test.rb  
> > \>  
> > \> require 'private.rb'  
> > \> require 'test/unit'  
> > \>  
> > \> class PrivateMethodTestTest \< Test::Unit::TestCase  
> > \>  
> > \> def test\_publicMethod  
> > \> var = PrivateMethodTest.new()  
> > \> assert\_equal('haha! test', var.publicMethod('test'))  
> > \> end  
> > \> end  
> > \>  
> > \> Now the question: why is it, that when the self. (bolded in the code) is  
> > \> there, the test fails, whereas if I remove it, the test is successful?  
> > This  
> > \> is the failure message:  
> > \>  
> > \> 1) Error:  
> > \> test\_publicMethod(PrivateMethodTestTest):  
> > \> NoMethodError: private method `privateMethod' called for  
> > \> #\<PrivateMethodTest:0x2b0876d805a0\>  
> > \> ./private.rb:9:in `publicMethod'  
> > \> private\_test.rb:8:in `test\_publicMethod'  
> > \>  
> > \> What am I missing?  
> > \>  
> > \> Thanks for any help and sorry if this question is silly 🙂  
> > \> Cheers,  
> > \> Chris
> > 
> > Ruby implements private methods as methods which can't be called using an  
> > explicit receiver (i.e, using the form obj.method\_name), but only using  
> > the  
> > implicit receiver (that is, method\_name). Since the implicit receiver is  
> > always self, this ensures that private instance methods can only be called  
> > by  
> > instance methods of that class (or of derived classes) and can't be  
> > accessed  
> > by other instances of the same class. An unpleasant side effect of this is  
> > that you can't call private methods using the explicit receiver even when  
> > that  
> > receiver is equal to the implicit one (that is, self), which is the  
> > situation  
> > you discovered.

---

<div class="post-metadata">

**Author:** ![Thufir](https://avatars.discourse-cdn.com/v4/letter/t/ee7513/32.png) [@Thufir](https://rubytalk.org/u/Thufir)\
**Post date:** [28 March 2008 06:59 UTC](https://rubytalk.org/t/newbie-question-private-public-methods/45322/5 "2008-03-28T06:59:55Z")

</div>

see:

[http://support.microsoft.com/kb/317591](http://support.microsoft.com/kb/317591)

for how enable quick edit and copy from the command prompt in Windows 🙂

-Thufir

> **···**
>
> On Fri, 21 Mar 2008 23:29:08 +0900, Mateusz Tybura wrote:
> 
> > I've get some other error form this code. Check at:  
> > [ImageShack - Best place for all of your image hosting and image sharing needs](http://img137.imageshack.us/img137/5754/errorxb0.jpg)

---

<div class="post-metadata">

**Author:** ![Stefano\_Crocco](https://avatars.discourse-cdn.com/v4/letter/s/5f9b8f/32.png) [@Stefano\_Crocco](https://rubytalk.org/u/Stefano_Crocco)\
**Post date:** [21 March 2008 15:16 UTC](https://rubytalk.org/t/newbie-question-private-public-methods/45322/6 "2008-03-21T15:16:39Z")

</div>

Do you refer to being unable to write self.a\_private\_method ? I don't think it  
can cause any trouble. It does prevent you from writing code like the  
following:

class C  
&nbsp;&nbsp;  
&nbsp;&nbsp;def initialize  
&nbsp;&nbsp;&nbsp;&nbsp;@x = 2  
&nbsp;&nbsp;end

&nbsp;&nbsp;def a\_method value  
&nbsp;&nbsp;&nbsp;&nbsp;self.x = value  
&nbsp;&nbsp;end

&nbsp;&nbsp;private  
&nbsp;&nbsp;  
&nbsp;&nbsp;def x= value  
&nbsp;&nbsp;&nbsp;&nbsp;#set the value of @x to something and do some other thing  
&nbsp;&nbsp;end

end

What the code is trying to do is define a private method, x= which changes the  
value of the instance variable @x and does some extra processing, then use  
this method every time it needs to change the value of @x. However, this kind  
of setter methods (those ending in =) need to be called with an explicit  
receiver, since otherwise ruby thinks we want to create the local variable x.  
But, since x= is a private method, it can't be called with an explicit  
receiver, either.

Of course, this is not a big problem: we have to call the method with another  
name (set\_x, for example) and everything works again, even if it's not as  
pretty as it would have been with x=.

Stefano

> **···**
>
> On Friday 21 March 2008, Krzysieq wrote:
> 
> > Then it enforces habits, which I've been encouraged to develop anyways - my  
> > managers always told me not to use this.doSomething whenever there is no  
> > risk of ambiguity. Don't You think however, that this is quite harsh - I  
> > mean, don't You think this might get someone into bigger trouble sometime?  
> > I don't know Ruby well enough to think of an example yet, but something  
> > tells me this "feature" could be tricky...

---

<div class="post-metadata">

**Author:** ![Krzysieq](https://avatars.discourse-cdn.com/v4/letter/k/71e660/32.png) [@Krzysieq](https://rubytalk.org/u/Krzysieq)\
**Post date:** [21 March 2008 15:28 UTC](https://rubytalk.org/t/newbie-question-private-public-methods/45322/7 "2008-03-21T15:28:59Z")

</div>

Well, my story is slightly different. I encountered this in a situation,  
when I had a private method to do some operations, and a number of public  
methods to use that one. And these public methods can't call the private  
using self. It's just something I'm used to doing, and perhaps now I will  
need to get unused to it 🙂 Maybe it's not dangerous at all, it's just  
different from what I've seen so far. And people tend to fear what they do  
not understand...

Thanks for explanations.  
Cheers,  
Chris

> **···**
>
> 2008/3/21, Stefano Crocco \<stefano.crocco@alice.it\>:
> 
> > On Friday 21 March 2008, Krzysieq wrote:  
> > \> Then it enforces habits, which I've been encouraged to develop anyways -  
> > my  
> > \> managers always told me not to use this.doSomething whenever there is no  
> > \> risk of ambiguity. Don't You think however, that this is quite harsh - I  
> > \> mean, don't You think this might get someone into bigger trouble  
> > sometime?  
> > \> I don't know Ruby well enough to think of an example yet, but something  
> > \> tells me this "feature" could be tricky...
> > 
> > Do you refer to being unable to write self.a\_private\_method ? I don't  
> > think it  
> > can cause any trouble. It does prevent you from writing code like the  
> > following:
> > 
> > class C
> > 
> > &nbsp;&nbsp;def initialize  
> > &nbsp;&nbsp;&nbsp;&nbsp;@x = 2  
> > &nbsp;&nbsp;end
> > 
> > &nbsp;&nbsp;def a\_method value  
> > &nbsp;&nbsp;&nbsp;&nbsp;self.x = value  
> > &nbsp;&nbsp;end
> > 
> > &nbsp;&nbsp;private
> > 
> > &nbsp;&nbsp;def x= value  
> > &nbsp;&nbsp;&nbsp;&nbsp;#set the value of @x to something and do some other thing  
> > &nbsp;&nbsp;end
> > 
> > end
> > 
> > What the code is trying to do is define a private method, x= which changes  
> > the  
> > value of the instance variable @x and does some extra processing, then use  
> > this method every time it needs to change the value of @x. However, this  
> > kind  
> > of setter methods (those ending in =) need to be called with an explicit  
> > receiver, since otherwise ruby thinks we want to create the local variable  
> > x.  
> > But, since x= is a private method, it can't be called with an explicit  
> > receiver, either.
> > 
> > Of course, this is not a big problem: we have to call the method with  
> > another  
> > name (set\_x, for example) and everything works again, even if it's not as  
> > pretty as it would have been with x=.
> > 
> > Stefano
