[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

why private is not private

Li Chen

12/17/2006 4:38:00 PM

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

--
Posted via http://www.ruby-....

8 Answers

David Vallner

12/17/2006 4:44:00 PM

0

Li Chen wrote:
> 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
>

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 Vallner

Pat Maddox

12/17/2006 5:24:00 PM

0

On 12/17/06, David Vallner <david@vallner.net> wrote:
> Li Chen wrote:
> > 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
> >
>
> 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 Vallner

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

Li Chen

12/17/2006 5:45:00 PM

0


> 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

--
Posted via http://www.ruby-....

Li Chen

12/17/2006 5:51:00 PM

0

Pat Maddox 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

--
Posted via http://www.ruby-....

David Vallner

12/17/2006 6:05:00 PM

0

Li Chen wrote:
> Pat Maddox 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
>
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 Vallner

Tim Hunter

12/17/2006 6:09:00 PM

0

Li Chen wrote:
> Pat Maddox 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
>
>
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".


Wayne Vucenic

12/24/2006 12:58:00 AM

0

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 Vucenic
No Bugs Software
Ruby, C#, and Erlang Agile Contract Programming in Silicon Valley

Li Chen

12/24/2006 3:20:00 PM

0

Wayne Vucenic 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

--
Posted via http://www.ruby-....