From: dblack@... Date: 2006-02-25T05:58:15+09:00 Subject: Re: Private methods - only available to oneself? Hi -- On Sat, 25 Feb 2006, James Byrne wrote: > Hal Fulton wrote: >> Minkoo Seo wrote: >>> Thanks, Erik. I'm afraid that I'm not a native English spearker, so >>> sometimes it's not easy to express my own idea in exact English >>> expression. >>> >>> Of course, I did look up the reference and found what instance_eval >>> does when being called. What I tried to ask was, as you stated, >>> "If we can do this, what's the purpose of 'protected' or 'private'?" >> >> 'private' is not like a locked door. It is like a sign saying "Do Not >> Enter.' >> >> Or look at it this way: It makes it "more difficult" to access private >> vars (so that you will know you shouldn't), but doesn't make it >> impossible (in case you really, really need to). >> >> >> Hal > > I do not understand why within the class definiton the Ruby interpreter > distinguishes between implicit and explict calls to self. > > i.e > > Class Foo > > def foo_one > bar > end > > def foo_two > self.bar > end > > def bar > puts "In Bar" > end > > private :bar > > end > > f = Foo.new > > f.foo_one > -> "In Bar" > > f.foo_two > -> NoMethodError: private method `bar' called for > > This leads to the inference that self !=== which strikes > me as decidedly odd. Self is either always self or it is not and if not > then what is it? Whether self is declared as the the receiver or left > for the interpreter to contextually establish should make no difference > to the effect of a private method. Is this difference due to a parsing > limitation or a purposeful design decision that serves some intent I > cannot presently fathom? If the latter, then what is the purpose served > by this disticntion between an explicit and implicit self receiver? I'm mostly guessing, but I imagine because it would be hard to establish that exception to the rule, and the exception would serve no purpose. Also, I think of self as a *representation* of the current object. So when the interpreter sees: x it doesn't literally stick "self" on the front; it just sends the message to the current object. David -- David A. Black (dblack@wobblini.net) Ruby Power and Light (http://www.rubypowerandlight.com) "Ruby for Rails" chapters now available from Manning Early Access Program! http://www.manning.com/books/black