# Confused about "private"?

**URL:** <https://www.ruby-forum.com/t/confused-about-private/54251>\
**Category:** Ruby\
**Created:** [February 27, 2006, 8:59pm UTC](https://www.ruby-forum.com/t/confused-about-private/54251 "2006-02-27T20:59:54Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_NSAlegre](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@John\_NSAlegre](https://www.ruby-forum.com/u/John_NSAlegre)\
**Post date:** [February 27, 2006, 8:59pm UTC](https://www.ruby-forum.com/t/confused-about-private/54251/1 "2006-02-27T20:59:54Z")

</div>

I have a confusion in Ruby’s way of dealing with “private” instance  
methods.

“Private” means that the method is accessible only within the class, and  
it  
is callable only in “function form,” with self (implicit or explicit) as  
a  
receiver." “Protected” gives access to the method in subclasses.

as is the case in Java or Obj C

However … here is some very simple code that is not doing what I  
expect …

####################################################

# Example One

# Works as expected

####################################################  
/usr/bin/ruby -w

class MyClass  
protected  
def say  
puts “Hello”  
end  
public  
def sayit  
self.say  
end  
end

class AClass \< MyClass  
end

puts “Start”  
r = MyClass.new  
r.sayit  
t = AClass.new  
t.sayit  
#########################################  
salamanca:~/Hack/Hack04/Ruby1 jna$ ./int.rb  
Start  
Hello  
Hello

That is exactly as I expected with the subclass having access to a super  
“protected” method.

####################################################

# Example Two

# Not as expected

####################################################  
/usr/bin/ruby -w

class MyClass  
private  
def say  
puts “Hello”  
end  
public  
def sayit  
self.say  
end  
end

class AClass \< MyClass  
end

puts “Start”  
r = MyClass.new  
r.sayit  
t = AClass.new  
t.sayit

#########################################  
salamanca:~/Hack/Hack04/Ruby1 jna$ ./int.rb  
Start  
…/int.rb:10:in `sayit': private method`say’ called for  
#MyClass:0x26148  
(NoMethodError)  
from ./int.rb:19

What I expected to see here is the first call to r.sayit retruning  
“Hello”  
as it is calling it’s own “private” method. I expected to see the error  
on  
the second call ot t.sayit.

What am I missing? This is not to the best of my knowledge the way  
things  
are in Java.

Thanks  
john

---

<div class="post-metadata">

**Author:** ![John\_NSAlegre](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@John\_NSAlegre](https://www.ruby-forum.com/u/John_NSAlegre)\
**Post date:** [February 27, 2006, 9:06pm UTC](https://www.ruby-forum.com/t/confused-about-private/54251/2 "2006-02-27T21:06:02Z")

</div>

Hi –

On Tue, 28 Feb 2006, John N. Alegre wrote:

> I have a confusion in Ruby’s way of dealing with “private” instance methods.
> 
> “Private” means that the method is accessible only within the class, and it  
> is callable only in “function form,” with self (implicit or explicit) as a  
> receiver." “Protected” gives access to the method in subclasses.

Private methods cannot be called with an explicit receiver, and “self”  
is an explicit receiver. The only exception is “setter” methods  
(ending with an equal sign), where you need an explicit receiver so  
that it isn’t taken for assignment to a variable.

There was a long thread about this in the past week or so – have a  
look.

> What am I missing? This is not to the best of my knowledge the way things  
> are in Java.

Why would it be? 🙂

David

–  
David A. Black (removed\_email\_address@domain.invalid)  
Ruby Power and Light ([http://www.rubypowerandlight.com](http://www.rubypowerandlight.com))

“Ruby for Rails” chapters now available  
from Manning Early Access Program! [Ruby for Rails](http://www.manning.com/books/black)

---

<div class="post-metadata">

**Author:** ![John\_NSAlegre](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@John\_NSAlegre](https://www.ruby-forum.com/u/John_NSAlegre)\
**Post date:** [February 28, 2006, 7:53am UTC](https://www.ruby-forum.com/t/confused-about-private/54251/3 "2006-02-28T07:53:53Z")

</div>

Here’s the link to the thread that Dave mentioned:  
[http://tinyurl.com/nhrn6](http://tinyurl.com/nhrn6)

Hope you enjoy 🙂

- Minkoo S.

---

<div class="post-metadata">

**Author:** ![John\_NSAlegre](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@John\_NSAlegre](https://www.ruby-forum.com/u/John_NSAlegre)\
**Post date:** [March 1, 2006, 4:16am UTC](https://www.ruby-forum.com/t/confused-about-private/54251/4 "2006-03-01T04:16:39Z")

</div>

Hi,

In message “Re: Confused about “private”??”  
on Wed, 1 Mar 2006 10:56:57 +0900, “John N. Alegre”  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

|I guess I will NOT open old wounds;) but I am not comfortable with Ruby’s  
|use of “private”. I do not see what it accomplishes by not letting me call  
|a private method within an object by using the self reference. I was  
|raised on Smalltalk and have come to habitually use self when I refer to  
|the insides of an Object. With Ruby I have to now be conscious NOT to do  
|that.

Unlike Smalltalk, Ruby has methods to be called like functions in  
other languages, for example, “print”. They are set private to avoid  
confusion.

```
						matz.

```

---

<div class="post-metadata">

**Author:** ![John\_NSAlegre](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@John\_NSAlegre](https://www.ruby-forum.com/u/John_NSAlegre)\
**Post date:** [March 1, 2006, 3:00am UTC](https://www.ruby-forum.com/t/confused-about-private/54251/5 "2006-03-01T03:00:29Z")

</div>

Minkoo,

Thanks …

I found it yesterday and read it.

I guess I will NOT open old wounds;) but I am not comfortable with  
Ruby’s  
use of “private”. I do not see what it accomplishes by not letting me  
call  
a private method within an object by using the self reference. I was  
raised on Smalltalk and have come to habitually use self when I refer to  
the insides of an Object. With Ruby I have to now be conscious NOT to  
do  
that.

Seams odd.

john

---

<div class="post-metadata">

**Author:** ![John\_NSAlegre](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@John\_NSAlegre](https://www.ruby-forum.com/u/John_NSAlegre)\
**Post date:** [March 1, 2006, 3:28pm UTC](https://www.ruby-forum.com/t/confused-about-private/54251/6 "2006-03-01T15:28:44Z")

</div>

Matz,

Thank you so much for taking the time to post this. It is great to get  
it  
from the source.

I understand your point and the self.call thing is a small thing to get  
used  
to considering the beauty of everything else.

Peace  
john
