From: David Flanagan Date: 2009-05-14T06:55:05+09:00 Subject: Re: Superclass of eigenclass On May 7, 5:35 am, Danny O cuiv wrote: > On page 261 of The Ruby Programming Language, they state: > > "Class objects are special: they have superclasses. The eigenclasses of > class objects are also special: they have superclasses, too. Theeigenclassof an ordinary object stands alone and has nosuperclass." The text you quote does not match the behavior of any version of Ruby I can find--I don't recall what I was thinking when I wrote this. I'll fix this. The description of how method lookup works (and how class methods are inherited) is conceptually correct, however. I think it is best to regard the superclass of an eigenclass as an implementation-defined construct, and not something you can rely on. As noted throughout this thread, Ruby has fuzzy boundaries between implementation and specification. And also, superclass and related methods play some tricks on you. The link you provide to an earlier ruby-talk message points out that the superclass of an eigenclass is one way in 1.8.2 and 1.9.x, and is another way in 1.8.6 and 1.8.7. My experimentation shows that jruby 1.0 works the way ruby 1.9 does in this regard. Let's discuss only Ruby 1.9 for now. In this version, I believe that if o is an object that is not a class, then: o.eigenclass.superclass == o.class Given this, then the method lookup algorithm for o is simple to describe. Begin with the eigenclass of o and climb the superclass chain until the method is found. (Where the superclass chain is not the same thing that is returned by the superclass method, because the superclass method skips over modules) Now suppose that o is the Fixnum class, and let's use ' to designate an eigenclass: Fixnum', Integer' and so on. In this case, methods are looked up in this hierarchy: Fixnum'->Integer'->Numeric'->Object'->BasicObject'->Class->Module- >Object->Kernel->BasicObject Notice that the eigenclass of the Comparable module is omitted here. Fixnum does not inherit class methods from ancestors that are modules. I believe that method inheritance works the same way in all recent versions of ruby, but that the observability of the inheritance chain through eigenclasses varies. Don't try this with Ruby 1.8.6 or Ruby 1.8.7. > This last sentence has me puzzled. The only possible interpretations > that I can imagine would be that callingsuperclasson theeigenclassof > an ordinary object would result in either (a) a method not found or (b) > the value nil being returned. However, neither of these is the case. > > Adding the usualeigenclassmethod to class Object, we then get in irb > (ruby 1.8.7 on Mac OS X): > > irb> greeting = "hello" > => "hello" > irb> greeting.eigenclass > => #> > irb> greeting.eigenclass.superclass > => # > > I interpret #> as "a class object for the > string object at 0xb7cf5a14". > I interpret # as "a class object for class String". > > This seems reasonable but conflicts with the description given in TRPL > at the top. Any ideas? > > Danny. > > P.S. In my searches before posting I came across the following that > no-one replied to. It seems that there are variations on the behaviour. > > http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/248178 > -- > Posted viahttp://www.ruby-forum.com/.