On Sun, 2002-12-01 at 20:44, Gavin Sinclair wrote: > That, to me, is obfuscated. 'self' is generally understood as the current > instance of the class. Yes. > Therefore, it is only meaningful inside instance methods. Well, not really. Self is available anytime there is a "current instance" active ... which is all the time in Ruby. At the top level, self refers to the "main" object ... traken$ irb irb(main):001:0> self main Inside a class definition, the current instance is the class itself! That is why attr_reader (and its friends) work. Bare code (i.e. anyting not inside a method definition) is live code that is executed. What is the object that handles method calls inside a class? It is the class object. So the current instance inside a class definition is the instance of the Class object being defined, in other words ... self. This is what allows things like "attr_reader" to work. attr_reader is a method defined in the class object. For example the following code ... class A attr_reader :prop end is equivalent to ... class A self.attr_reader :prop end (except that attr_reader is private and so won't work with an explicit target. You can fix this with: class Module; public :attr_reader; end) If self didn't refer to the class instance, the call to the attr_reader method wouldn't work. > There's a yawning gulf between syntax and semantics, and I suggest that it's > taking advantage of an undocumented feature. Actually, you can argue that there is the syntax and semantics are *tightly* coupled. Knowing what "class << obj" means and knowing that inside a class, self is the current class instance, directly leads to the meaning of "class << self". -- -- Jim Weirich jweirich / one.net http://w3.one.net/~jweirich --------------------------------------------------------------------- "Beware of bugs in the above code; I have only proved it correct, not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)