# Why private is not private

**URL:** <https://www.ruby-forum.com/t/why-private-is-not-private/79647>\
**Category:** Ruby\
**Created:** [December 17, 2006, 5:37pm UTC](https://www.ruby-forum.com/t/why-private-is-not-private/79647 "2006-12-17T17:37:39Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [December 17, 2006, 5:37pm UTC](https://www.ruby-forum.com/t/why-private-is-not-private/79647/1 "2006-12-17T17:37:39Z")

</div>

Hi all,

I add a private method to class Object and try to call it from outside  
the class. I am confused that it works. Any comments?

Thanks,

Li

## 

class Object  
private  
def method1(arg1)  
arg1  
end  
end

puts “call private method”  
puts method1(1)

## 

> ruby variables3.rb  
> call private method  
> 1  
> Exit code: 0

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [December 17, 2006, 5:44pm UTC](https://www.ruby-forum.com/t/why-private-is-not-private/79647/2 "2006-12-17T17:44:31Z")

</div>

Li Chen wrote:

> private  
> call private method  
> 1
> 
> > Exit code: 0

The toplevel execution context is also an instance of Object. You’re  
still inside an instance of the class you’ve defined #method1 on.

David V.

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [December 17, 2006, 6:24pm UTC](https://www.ruby-forum.com/t/why-private-is-not-private/79647/3 "2006-12-17T18:24:41Z")

</div>

On 12/17/06, David V. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > class Object
> > 
> > > ruby variables3.rb  
> > > call private method  
> > > 1  
> > > Exit code: 0
> 
> The toplevel execution context is also an instance of Object. You’re  
> still inside an instance of the class you’ve defined #method1 on.
> 
> David V.

Just to expand on David’s point a little…

Since everything is an object in Ruby, you have to be in SOME context  
relating to an object. Any time you call a method without an explicit  
receiver, the receiver defaults to self. So in that example you end  
up calling  
self.method1

which is fine, as you’d expect.

Also as you’d expect, instantiating a new Object and then calling the  
private method blows up:

irb(main):007:0\> o = Object.new  
=\> #Object:0x355410  
irb(main):008:0\> o.method1  
NoMethodError: private method `method1’ called for #Object:0x355410  
from (irb):8  
from :0

Pat

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [December 17, 2006, 6:42pm UTC](https://www.ruby-forum.com/t/why-private-is-not-private/79647/4 "2006-12-17T18:42:08Z")

</div>

Pat M. wrote:

> Just to expand on David’s point a little…
> 
> Since everything is an object in Ruby, you have to be in SOME context  
> relating to an object. Any time you call a method without an explicit  
> receiver, the receiver defaults to self. So in that example you end  
> up calling  
> self.method1
> 
> which is fine, as you’d expect.

One more question: in my previouse script what does self really refer  
to?

Thanks,

Li

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [December 17, 2006, 7:08pm UTC](https://www.ruby-forum.com/t/why-private-is-not-private/79647/5 "2006-12-17T19:08:50Z")

</div>

Li Chen wrote:

> > which is fine, as you’d expect.
> 
> One more question: in my previouse script what does self really refer  
> to?
> 
> Thanks,
> 
> Li

C:\Documents and Settings\David\>irb  
irb(main):001:0\> self  
=\> main  
irb(main):002:0\> self.class  
=\> Object  
irb(main):003:0\>

A “special” Object Ruby creates to evaluate everything else in. I don’t  
even think there’s that much special about it beyond what an Object.new  
call would do (aside from the fact it comes with the ruby core already  
loaded by the interpreter.)

David V.

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [December 17, 2006, 7:10pm UTC](https://www.ruby-forum.com/t/why-private-is-not-private/79647/6 "2006-12-17T19:10:51Z")

</div>

Li Chen wrote:

> > self.method1

Ask Ruby…

ruby$ irb  
irb(main):001:0\> self  
=\> main

The outermost object in Ruby is an instance of Object that irb  
identifies as “main”.

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [December 17, 2006, 6:31pm UTC](https://www.ruby-forum.com/t/why-private-is-not-private/79647/7 "2006-12-17T18:31:53Z")

</div>

> Also as you’d expect, instantiating a new Object and then calling the  
> private method blows up:
> 
> irb(main):007:0\> o = Object.new  
> =\> #Object:0x355410  
> irb(main):008:0\> o.method1  
> NoMethodError: private method `method1’ called for #Object:0x355410  
> from (irb):8  
> from :0

Thanks a lot, Pat. I am much clear about the concept now.

Li

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [December 24, 2006, 1:58am UTC](https://www.ruby-forum.com/t/why-private-is-not-private/79647/8 "2006-12-24T01:58:28Z")

</div>

Hi Li,

To add to what others have replied, “private” in Ruby  
means that the method cannot be called with an explicit receiver.  
So it can be called from a subclass (which wouldn’t work for  
private methods in C++/Java), but it can’t be called with  
an explicit “self.” (which would work in C++/Java):

class Object  
private  
def method1(arg1)  
arg1  
end  
end

class Test  
def test  
puts “calling method1 within Test…”  
puts method1(1)  
end  
end

t = Test.new  
t.test

puts “calling self.method1…”  
puts self.method1(2)

* * *

calling method1 within Test…  
1  
calling self.method1…  
F:/Temp/privateTest.rb:21: private method `method1’ called for  
main:Object (NoMethodError)

C:\>ruby -v  
ruby 1.8.2 (2004-12-25) [i386-mswin32]

Hope this helps,

Wayne

* * *

Wayne V.  
No Bugs Software  
Ruby, C#, and Erlang Agile Contract Programming in Silicon Valley

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [December 24, 2006, 4:19pm UTC](https://www.ruby-forum.com/t/why-private-is-not-private/79647/9 "2006-12-24T16:19:41Z")

</div>

Wayne V. wrote:

> To add to what others have replied, “private” in Ruby  
> means that the method cannot be called with an explicit receiver.  
> So it can be called from a subclass (which wouldn’t work for  
> private methods in C++/Java), but it can’t be called with  
> an explicit “self.” (which would work in C++/Java):

Thank you for the input and Merry X-mas to all,

Li
