From: Minkoo Seo Date: 2006-02-15T01:08:30+09:00 Subject: Re: Private methods - only available to oneself? gwtmp01@mac.com wrote: > On Feb 13, 2006, at 7:08 PM, Adam P. Jenkins wrote: > > I wasn't aware of singleton *methods*. I thought there were > > singleton *classes*, in which case the C++/Java definition of > > private would do just as well, since a particular object would be > > the only instance of the singleton class. That is: > > Well singleton methods are implemented by tucking them away in a > singleton class > but you don't have to know about that implementation to utilize the > facility: > > a = [1,2,3,4] > b = [5,6,7,8] > > def a.sum > inject(0) { |s, x| s + x } > end > > a.sum # -> 10 > b.sum # NoMethodError exception > > > I think this all might come into play when you consider 'class > methods'. In Ruby, classes are > objects and so class methods are really singleton methods associated > with a particular instance > of Class. If you didn't have Ruby's concept of private then any > class method could directly > call any other class method (because all classes are instances of > Class). I believe that the class method is implemented transparently in Ruby, because we do not have to metion 'private' to make a certain class method. Of course, it surely make sense that the class method could not be impelemented without the notion of private. However, I think class method example is not a thorough argument for private. I'm afraid that I still do not follow the line of reasoning. Well, this might be the result of my heavily java/c++ based way of thinking. I also do not see any other reason for using private when I code some programs (not the ruby itself). Why should a certain instance hide itself from the other instances of the same class? Could anyone give me an exmple? Best, Minkoo Seo