From: "Hal E. Fulton" Date: 2002-12-02T11:30:13+09:00 Subject: Re: class << self (was Re: [FAQ] What do some of Ruby's symbols mean?) ----- Original Message ----- From: "Gavin Sinclair" To: "ruby-talk ML" Sent: Sunday, December 01, 2002 7:44 PM Subject: Re: class << self (was Re: [FAQ] What do some of Ruby's symbols mean?) > > There's no arcane magic going on here - self is an object of class > > Class, and in this case it has the value Array. > > That, to me, is obfuscated. 'self' is generally understood as the current > instance of the class. Therefore, it is only meaningful inside instance > methods. In the "class << self" idiom, there is no instance method. 'self' > may be defined, but it's not *meaningful* in the sense described above. > There's a yawning gulf between syntax and semantics, and I suggest that it's > taking advantage of an undocumented feature. Hmmmmm. This always makes my brain itch, but I'll make a stab at it. I guess I would say that Ruby is OO "even when it isn't" -- in the sense that a "toplevel" method or constant belongs to Object. You may argue that it doesn't make sense -- and maybe you are right -- but I think "self" is always defined in Ruby. The one-line programs "puts self" and "puts self.class" will print "main" and "Object" respectively. Given this paradigm, it does arguably seem natural that in the middle of a class definition, "self" takes on the value of the class. And of course, Class#meth (instance method of Class) means that there will be a Foo.meth (class method of Foo) for every class Foo; and a singleton method for the singleton class of a class is the same as a class method for the class. Isn't it? ;) Hal