It’s well-known fact that everything in Ruby is an object or in other words instance of something, but it still could be confusing how class definitions and instances produced by them are about the same. In this post, I will try to make it cleaner.

class Person; end

This is the way how we create class in Ruby via using language syntax. At the same time, it is just a common way we create a new instance of Class and assigning it to Person variable, which is equal to:

Person = Class.new

Only instances of Class have abilities to create new instances:

person = Person.new

And someone could raise a question — if only instances produced by Class have the ability to create new instances then how we could explain the ability of having Class.new and produce such instances too? So, there is a logical answer — Class is the instance of Class or itself;)

person.class # => Person

Person.class # => Class

Class.class # => Class Class.object_id == Class.class.object_id # => true

The question of how Class could be an instance of them-self is rhetorical because it’s language implementation about which could be more discovered in Ruby source code, also there is an awesome book Ruby Under a Microscope: An Illustrated Guide to Ruby Internals.

Instances of Class we simply call “classes” as places of definitions for our instances which they could produce, at the same time we name our “classes” with capital letter as Person to highlight the fact that it’s Class instance, but as it’s still an instance it could have any name:

person = Class.new # => #<Class:0x00005614bb425718>

Person = Class.new # => Person

The difference is only the way how such instance represent/print itself, so if we will ignore such customization there will be no difference: