# Private encapsulation question

**URL:** <https://www.ruby-forum.com/t/private-encapsulation-question/128685>\
**Category:** Ruby\
**Created:** [February 10, 2008, 1:56pm UTC](https://www.ruby-forum.com/t/private-encapsulation-question/128685 "2008-02-10T13:56:10Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![andrea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c37758/32.png) [@andrea](https://www.ruby-forum.com/u/andrea)\
**Post date:** [February 10, 2008, 1:56pm UTC](https://www.ruby-forum.com/t/private-encapsulation-question/128685/1 "2008-02-10T13:56:10Z")

</div>

Hello,  
I am trying to improve my understanding of encapsulation but I can’t  
understand the reason why the last 2 method calls in the following code  
produce different results (cut and paste in irb, plus return key):

class N  
def initialize(n)  
@number=n  
end

def self\_n\_wrapper  
self.n  
end

def n\_wrapper  
n  
end

private

def n  
@number  
end

end

a=N.new(8)  
a.n\_wrapper  
a.self\_n\_wrapper

Thanks for help  
Andrea

–

> **[Andrea's Retrocomputing Homepage](http://myretrocomputing.altervista.org)**
>
> Sito personale di retrocomputing a cura di Longhi Andrea

---

<div class="post-metadata">

**Author:** ![andrea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c37758/32.png) [@andrea](https://www.ruby-forum.com/u/andrea)\
**Post date:** [February 10, 2008, 2:09pm UTC](https://www.ruby-forum.com/t/private-encapsulation-question/128685/2 "2008-02-10T14:09:49Z")

</div>

Alle Sunday 10 February 2008, andrea ha scritto:

> def self\_n\_wrapper  
> @number  
> Andrea  
> Ruby implements private methods forbidding to call them with an explicit  
> receiver, but only with the explicit receiver, self. This is enough to  
> ensure  
> a private instance method of class A can’t be called by, for example an  
> instance method of class B, because in the latter self is an instance of  
> class  
> B, not of class A. For a similar reason, you can call that method from  
> an  
> instance method of a class C derived from A. A side effect of this  
> approach is  
> that you can’t call a private method using the explicit receiver self,  
> even if

self.my\_method

is the same as

my\_method

I hope this helps

Stefano

---

<div class="post-metadata">

**Author:** ![andrea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c37758/32.png) [@andrea](https://www.ruby-forum.com/u/andrea)\
**Post date:** [February 10, 2008, 2:15pm UTC](https://www.ruby-forum.com/t/private-encapsulation-question/128685/3 "2008-02-10T14:15:41Z")

</div>

> why the last 2 method calls

Only a partial answer: protected instead of private should work.

---

<div class="post-metadata">

**Author:** ![andrea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c37758/32.png) [@andrea](https://www.ruby-forum.com/u/andrea)\
**Post date:** [February 10, 2008, 4:15pm UTC](https://www.ruby-forum.com/t/private-encapsulation-question/128685/4 "2008-02-10T16:15:51Z")

</div>

andrea wrote:

> def self\_n\_wrapper  
> self.n  
> end

> a.self\_n\_wrapper  
> private method `n’ called

Calling self.n is equivalent to calling a.n here, isn’t it?

---

<div class="post-metadata">

**Author:** ![andrea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c37758/32.png) [@andrea](https://www.ruby-forum.com/u/andrea)\
**Post date:** [February 10, 2008, 9:20pm UTC](https://www.ruby-forum.com/t/private-encapsulation-question/128685/5 "2008-02-10T21:20:38Z")

</div>

Stefano C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Ruby implements private methods forbidding to call them with an explicit  
> receiver, but only with the explicit receiver, self. This is enough to ensure  
> a private instance method of class A can’t be called by, for example an  
> instance method of class B, because in the latter self is an instance of class  
> B, not of class A.

I don’t know if I understand correctly your last sentence, but also less  
rescrictive (compared to private) protected methods behave the way you  
described, am I right? I am asking because I don’t get the link between  
the 2 sentences quoted above.  
Sorry, while the rest of your post is pretty clear it seems I am missing  
something here… any further explaination is welcome 🙂

Bye  
Andrea

–

> **[Andrea's Retrocomputing Homepage](http://myretrocomputing.altervista.org)**
>
> Sito personale di retrocomputing a cura di Longhi Andrea

---

<div class="post-metadata">

**Author:** ![andrea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c37758/32.png) [@andrea](https://www.ruby-forum.com/u/andrea)\
**Post date:** [February 10, 2008, 11:01pm UTC](https://www.ruby-forum.com/t/private-encapsulation-question/128685/6 "2008-02-10T23:01:27Z")

</div>

Stefano C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

[cut]

> I hope this makes things clearer

Yes, definitely. Now I get the point.  
Thanks again.

Andrea

–

> **[Andrea's Retrocomputing Homepage](http://myretrocomputing.altervista.org)**
>
> Sito personale di retrocomputing a cura di Longhi Andrea

---

<div class="post-metadata">

**Author:** ![andrea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c37758/32.png) [@andrea](https://www.ruby-forum.com/u/andrea)\
**Post date:** [February 10, 2008, 10:09pm UTC](https://www.ruby-forum.com/t/private-encapsulation-question/128685/7 "2008-02-10T22:09:34Z")

</div>

Alle Sunday 10 February 2008, andrea ha scritto:

> the 2 sentences quoted above.  
> Sorry, while the rest of your post is pretty clear it seems I am missing  
> something here… any further explaination is welcome 🙂
> 
> Bye  
> Andrea

I agree that what I wrote isn’t very clear. This is what I meant: a  
private  
method can only be called without explicitly using a receiver, that is:

my\_method(1,2,3)

is OK, but

something.my\_method(1,2,3)

is wrong. This means that, if my\_method is a private instance method of  
class  
A, i can only call it from places where self is an instance of class A  
because, if I try to call it from somewhere else, I need an explicit  
receiver,  
which doesn’t work for private methods. The weird thing is that even  
self.my\_method uses an explicit receiver and so causes an error, even if  
the  
receiver is the same as the explicit receiver.

There are a couple of other peculiarities which arise from this approach  
to  
private methods, compared with other languages, such as C++

- you can only call a private method of self, not of another object of  
the  
same class (because you’d need to use an explicit receiver to do so)
- you can can call a private method from a derived class (because you  
don’t  
need an explicit receiver).

Here there are some examples:

class A

def m1  
end  
private :m1

def m2  
m1 #this works, because I’m using the implicit receiver, self  
end

def m3  
#this doesn’t work because I’m using an explicit receiver, even  
#if it’s the same object as the implicit receiver  
self.m1  
end

def m4 other  
#this doesn’t work, regardless of whether other is an instance of A  
#or not (of course, provided it doesn’t have a public m1 method)  
other.m1  
end

end

A.new.m1 #this doesn’t work because of explicit receiver

class B \< A

def m5  
m1 #this works because I don’t need to use an explicit receiver  
end

end

I hope this makes things clearer

Stefano
