This chapter explains how Ruby uses classes to organize objects so you can create many similar instances without hand-crafting each one separately. A class groups shared behavior into a reusable blueprint, and instances created from it inherit those behaviors automatically. The chapter also emphasizes that Ruby is fundamentally object-centered: nearly everything is an object, classes themselves are objects, and even basic class creation and method dispatch follow the same message-sending model as ordinary objects.
It then shows how objects store and expose state through instance variables and methods. Instance variables, marked with @, preserve data across method calls and let each object keep its own information. The chapter builds on that by introducing initialize for setting state at creation time, then explores setter methods for changing state later, including Ruby’s special assignment-like method names such as price= and the syntactic sugar that lets you write ticket.price = 63.00. To reduce repetition, Ruby provides attr_reader, attr_writer, and attr_accessor, which automatically generate common getter and setter methods.
Finally, the chapter broadens the discussion to inheritance, class methods, and constants. It explains single inheritance, where subclasses gain methods from superclasses and Ruby’s class hierarchy ultimately leads through Object and BasicObject. It also shows that classes are objects too, which is why they can receive messages and define class methods such as constructors or utility routines. The chapter closes by covering constants, their lookup syntax, and the distinction between reassigning a constant name and modifying the object it references, while underscoring Ruby’s flexibility: an object’s behavior comes from its class and ancestors, but can also be extended dynamically during runtime.
Summary
Writing a class and then creating instances of that class allows you to share behaviors among numerous objects.
Use setter and getter methods, either written out or automatically created with the attr_* family of methods, to create object attributes, which store an object’s state in instance variables.
As objects, classes can have methods added to them on a per-object basis—such methods being commonly known as class methods, and providing general utility functionality connected with the class.
Ruby constants are a special kind of identifier usually residing inside class or module definitions.
Inheritance is a class-to-class relationship between a superclass and one or more subclasses.
All Ruby objects have a common ancestry in the Object and BasicObject classes.
The superclass/subclass structure can lend itself to modeling entities in a strictly hierarchical, taxonomical way, but the dynamic qualities of Ruby objects (including class objects) can offer less strictly determined ways of thinking about objects and how their behaviors might unfold over the course of their lives.
FAQ
What is a class in Ruby, and why do we use classes instead of defining methods on individual objects one by one?Classes are blueprints for creating many objects that share the same behavior. Instead of attaching methods separately to each object, you define them once in a class and then instantiate as many objects as you need. This makes programs easier to organize and scale.What is the difference between a class and an instance?A class is the template or definition; an instance is a specific object created from that class. For example, Ticket is a class, and Ticket.new creates a ticket instance.What is an instance method?An instance method is a method defined inside a class that can be called by every object created from that class. For example, if event is defined inside Ticket, then every ticket object can call event.What happens when you define the same method twice in a class?The later definition overrides the earlier one. Ruby uses the most recent version of the method, so the second definition takes precedence.Why and how can a class be reopened in Ruby?Ruby lets you open a class definition more than once and add or change methods. This can be useful when code for a class is spread across files or when you’re extending built-in classes like Time.What are instance variables, and how do they relate to object state?Instance variables store data that belongs to a particular object, such as a ticket’s venue or price. They begin with @ and persist between method calls, which makes them the main way objects remember their state.What is the purpose of the initialize method?initialize runs automatically whenever a new object is created with new. It is commonly used to set up the object’s initial state by assigning values to instance variables.What is a setter method, and why does Ruby allow method names that end with =?A setter method is used to change an object’s state, usually by assigning a value to an instance variable. Ruby allows method names like price=, and it also provides assignment-like syntax such as ticket.price = 63.00, which is really a method call in disguise.What are attr_reader, attr_writer, and attr_accessor?These are shortcuts for creating attribute methods. attr_reader creates a reader method, attr_writer creates a writer method, and attr_accessor creates both a reader and writer for the same attribute.How does inheritance work in Ruby, and what does it mean for one class to inherit from another?Inheritance means a subclass gets the methods of its superclass and its earlier ancestors. For example, if Magazine inherits from Publication, then magazine objects can use publication methods too. Ruby supports only single inheritance, so each class can have only one superclass.
pro $24.99 per month
access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!