Why private is not private

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

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.

On 12/17/06, David V. [email protected] 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

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

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.

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”.

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

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

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