First, let's add a method to retrieve eigenclass "copied from this blog post"
class Object
def eigenclass
class << self
self
end
end
end
Then create a simple class
class A
end
puts A.new.eigenclass.superclass # => A
puts Class.new.eigenclass.superclass # => #<Class:Object>
I was expecting the second puts to output Class
Any clue why this happened?
From that blogpost, you can construct a similar diagram:
Trying to find the superclass of the eigenclass of an instance of A shows that it points to the
A
class.Class.new
returns an instance of a Class object, i.e. a new class. It is a class, just like theA
class.A's superclass and Class.new's superclass are both implicitly
Object
.Because A's superclass is
Object
, A's eigenclass's superclass is Object's eigenclass.Similarly, finding the superclass of the eigenclass of
Class.new
yields Object's eigenclassThe difference between
Class.new
andA.new
is thatClass.new
is itself a class and so can construct new objects, whileA.new
cannot.