DEV Community

loading...

Understanding the Eigenclass in less than 5 minutes

chenge
Ruby, and learn Rust, Go, Elixir, Erlang...
・1 min read

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)

Forem Open with the Forem app