Why isn't the eigenclass equivalent to self.class, when it looks so similar?

Viewed 16576

I've missed the memo somewhere, and I hope you'll explain this to me.

Why is the eigenclass of an object different from self.class?

class Foo
  def initialize(symbol)
    eigenclass = class << self
      self
    end
    eigenclass.class_eval do
      attr_accessor symbol
    end
  end
end

My train of logic that equates the eigenclass with class.self is rather simple:

class << self is a way of declaring class methods, rather than instance methods. It's a shortcut to def Foo.bar.

So within the reference to the class object, returning self should be identical to self.class. This is because class << self would set self to Foo.class for definition of class methods/attributes.

Am I just confused? Or, is this a sneaky trick of Ruby meta-programming?

3 Answers
Related