liveBook · Manning

Class

As explained in The Well-Grounded Rubyist, Fourth Edition

Class is Ruby's built-in class whose instances represent classes and whose behavior supports class-level operations and instantiation.

Need many objects with the same behavior? Define a class rather than adding methods to each object separately. `Ticket.new` sends `new` to the class object `Ticket` and returns a new instance. Methods defined in `Ticket` can then serve every ticket, while each ticket remains a distinct object. section 3.0 section 3.1

A class creates instances

`class Ticket; def event; "Concert"; end; end` defines an instance method. After `ticket = Ticket.new`, `ticket.event` returns `"Concert"`. The receiver of `new` is the class; the receiver of `event` is the ticket. section 3.1

`Ticket` is itself an object: an instance of Ruby’s built-in class `Class`. A class can receive messages, be passed as a method argument, and have methods added to it. `Class.new` creates a class object, just as `Ticket.new` creates a ticket. The new class need not immediately have a constant name: `my_class = Class.new` followed by `my_class.new` creates an instance through a local variable. section 3.6 section 3.6.1

c = Class.new do
  def say_hello
    puts "Hello!"
  end
end

The `Class.new` block defines an instance method while creating an anonymous class; instances made with `c.new` can call `say_hello`. section 3.6.1

Class objects get methods much as other objects do. `Ticket.new` works because `new` is an instance method of `Class`. `Class` inherits from `Module`, so a class object can also use methods such as `attr_reader` and `attr_accessor`. Inside a class definition, the class object is `self`: bare calls to those attribute methods go to that object. section 3.6.2 section 5.1.1

A method on the class object is not automatically a method on its instances. If `Ticket.most_expensive` exists, calling `most_expensive` on an individual ticket fails unless that ticket has an applicable method of its own. This distinction separates class-level utility behavior from instance behavior. section 3.6.5

Classes can be reopened: two `class C` blocks that define different methods add both methods to `C`. This can support deliberate extensions across files, but scattered definitions can be harder to follow. Classes can also be nested in modules and named through constants such as `Tools::Hammer`; that notation alone does not reveal whether a named constant holds a class or another kind of value. section 3.1.3 section 4.4.2

How The Well-Grounded Rubyist, Fourth Edition covers it

How deep it goes
Works through `Class.new`, instantiation, and class-object method lookup, including inherited methods and the role of `self`.
Examples
Uses ticket instances, an anonymous class with `say_hello`, and reopened class definitions.
What it assumes
Assumes familiarity with objects and method calls while defining how classes create instances and receive messages.
What you can do after
Build it from scratch with `class` or `Class.new`, then distinguish methods called on a class from methods called on its instances.
How it connects
Connects classes to objects, instance methods, class methods, `Module`, inheritance, constants, and `self`.

Also explained in

Compare how 21 books explain class →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Well-Grounded Rubyist, Fourth Edition. The section links point to the parts of the book it draws on.