Classes for Rubys
Ruby classes and modules are how programs model objects and share behaviour. A class defines instance variables and methods and can inherit from one parent, while a module is a bundle of methods mixed in with include or extend. This unit also touches metaprogramming, where code defines or inspects methods at runtime.
Stage: Objects · Part of the Ruby roadmap · Last reviewed 19 November 2026
What to learn in Classes
- Define a class with initialize, instance variables and accessors
- Share behaviour across classes by mixing in a module
- Explain inheritance and call super from a subclass
- Control access with public, private and protected
- Use define_method or method_missing in a small, careful example
Classes interview questions
Why does Ruby use modules as mixins instead of multiple inheritance?
A class can have only one superclass, but it can include many modules. Mixins give reusable behaviour, like Comparable or Enumerable, without the ambiguity of multiple inheritance. The ancestors list shows the order in which methods are looked up.
What does attr_accessor do?
It generates a getter and a setter method for an instance variable, so attr_accessor :name creates name and name=. attr_reader makes only the getter and attr_writer only the setter. This avoids writing simple accessor methods by hand.
Where Classes fits in the Ruby roadmap
Classes belongs to the Objects stage (beginner level), which takes about 17 hours. Object-oriented Ruby.