3.1. Classes and instances

A typical class consists of a collection of method definitions. Classes usually exist for the purpose of being instantiated—that is, of having objects created that are instances of the class.

You’ve already seen instantiation in action. It’s our old signature tune:

obj = Object.new

Object is a built-in Ruby class. When you use the dot notation on a class, you send a message to the class. Classes can respond to messages, just like objects; in fact, as you’ll have reason to be aware of in any number of situations, classes are objects. The new method is a constructor: a method whose purpose is to manufacture and return to you a new instance of the class, a newly minted object.

You define a class with the class keyword. ...

Get The Well-Grounded Rubyist, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.