D
Danny O cuiv
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. The
eigenclass of an ordinary object stands alone and has no superclass."
This last sentence has me puzzled. The only possible interpretations
that I can imagine would be that calling superclass on the eigenclass of
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 usual eigenclass method to class Object, we then get in irb
(ruby 1.8.7 on Mac OS X):
irb> greeting = "hello"
=> "hello"
irb> greeting.eigenclass
=> #<Class:#<String:0xb7cf5a14>>
irb> greeting.eigenclass.superclass
=> #<Class:String>
I interpret #<Class:#<String:0xb7cf5a14>> as "a class object for the
string object at 0xb7cf5a14".
I interpret #<Class:String> 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
"Class objects are special: they have superclasses. The eigenclasses of
class objects are also special: they have superclasses, too. The
eigenclass of an ordinary object stands alone and has no superclass."
This last sentence has me puzzled. The only possible interpretations
that I can imagine would be that calling superclass on the eigenclass of
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 usual eigenclass method to class Object, we then get in irb
(ruby 1.8.7 on Mac OS X):
irb> greeting = "hello"
=> "hello"
irb> greeting.eigenclass
=> #<Class:#<String:0xb7cf5a14>>
irb> greeting.eigenclass.superclass
=> #<Class:String>
I interpret #<Class:#<String:0xb7cf5a14>> as "a class object for the
string object at 0xb7cf5a14".
I interpret #<Class:String> 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