From: dblack@... Date: 2007-09-03T02:59:18+09:00 Subject: Re: Shouldn't it be class >> self , or am I dyslexic? Hi -- On Mon, 3 Sep 2007, Ben Tompkins wrote: > I like it when people agree with me, and according to the diagram on > page 384 of Programming Ruby, you are basically correct to say that > "self inherits from the singleton class." The diagram shows that a > "virtual" (singleton) class is inserted into the inheritance hierarchy > for self (or whatever the RHS happens to be) as an immediate subclass of > the most derived, non-singleton, class of which the RHS is an instance > whenever 'class << RHS ...' is executed. So even if we think of the '<<' > as an "inheritance operator," it is still pointing in the wrong > direction! But is inheritance really the right concept here? Notice that > the singleton class is inserted below the most specific class of the RHS > term. If the RHS were the same as its class (which is impossible), then > Marcin would be right, because the singleton class would be subclassing > the RHS. But no instance is ever the same as its class. What is really > going on is that we are > augmenting the interface of the RHS by decorating its most derived class > with an anonymous class to which RHS has exclusive access, thus > effectively adding the methods of the singleton class to the RHS. As the > recipient of these new methods, RHS is on the receiving side of the > operation, which is why 'class >> RHS ...' is more intuitive than 'class > << RHS ... .' I'm sorry, but I had to chuckle at the use of the word "intuitive" after that explanation :-) I wouldn't think of class << obj as having anything at all to do with inheritance. It's more like this, in terms of how the class keyword works: class expr where expr can be: A A < B << arbitrary_object So the inheritance < is more like: class (A < B) and the singleton notation is more like: class (<< obj) Remember, also, that the class keyword isn't itself a class identifier; you're not looking for "the class that is >> object", but the class that is, so to speak, yielded from object. (Or something.) It's not a notation that relates very closely to any other. David -- * Books: RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242) RUBY FOR RAILS (http://www.manning.com/black) * Ruby/Rails training & consulting: Ruby Power and Light, LLC (http://www.rubypal.com)