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

Posted by The Wicked Flea on Stack Overflow See other posts from Stack Overflow or by The Wicked Flea
Published on 2009-10-27T13:31:13Z Indexed on 2010/04/05 21:13 UTC
Read the original article Hit count: 159

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?

© Stack Overflow or respective owner

Related posts about ruby

Related posts about language-features