Singleton Classes
A singleton method is a method that belongs to a single object. A singleton class, on the other hand, is a class that defines a single object. Confused? Me too. Let’s take a closer look.
Let’s suppose you create a few dozen objects, each of which is an instance of the Object class. Naturally they all have access to inherited methods such as inspect and class. But now you decide that you want just one special object (for the sake of variety, let’s call him ob), which has one special method (let’s call it blather).
You don’t want to define a whole new class for this one object since you will never again create any more objects with the blather method. So, you create a class especially for little ob.
You don’t need to name this class. ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access