Basic Inheritance

To understand inheritance, you have to start with a base class:

class ParentClass      def this           # do whatever      end      def that           # do whatever      endend

The syntax for deriving a class from that class is:

class ChildClass < ParentClassend

The ChildClass definition indicates that it inherits from ParentClass. This means that even though ChildClass doesn’t define any of its own methods, it still has two: this and that. You can now do:

ex = ChildClass.newex.thisex.that

There’s no limit to how many classes can be derived from a superclass (Figure 8.1) or how many tiers of inheritance can be created (Figure 8.2). However, as a rule of thumb, a well-conceived model probably won’t involve more than ...

Get Ruby: Visual Quickstart Guide 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.