DEV Community

chenge
chenge

Posted on

Understanding the Eigenclass in less than 5 minutes

Today I read Understanding the Eigenclass in less than 5 minutes. It explained clearly.

class Greeting
  def self.hello
    'hello world!'
  end

  def self.eigenclass
    class << self
      self
    end
  end
end

Greeting.eigenclass      # => #<Class:Greeting>
Greeting.eigenclass.name # => nil

Greeting.singleton_methods                  # => [:hello, :eigenclass]
Greeting.eigenclass.instance_methods(false) # => [:hello, :eigenclass]
class << self

This weird syntax means the eigenclass context.

Discussion (0)