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
Aclass.Class.newreturns an instance of a Class object, i.e. a new class. It is a class, just like theAclass.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.newyields Object's eigenclassThe difference between
Class.newandA.newis thatClass.newis itself a class and so can construct new objects, whileA.newcannot.