🎉 Hooray! After 3 years of work, I've finally released the ebook on design patterns! Check it out »

Singleton in Ruby

Singleton is a creational design pattern, which ensures that only one object of its kind exists and provides single point of access to it for any other code.

Singleton has almost the same pros and cons as global variables. Although they’re super-handy, they break modularity of your code.

You can just use a class, which depends on Singleton, in some other context. You’ll have to carry the Singleton class as well. Most of the time, this limitation comes up during the creation of unit tests.

Learn more about Singleton

Usage of the pattern in Ruby



Usage examples: A lot of developers consider the Singleton pattern an antipattern. That’s why its usage is on the decline in Ruby code.

Identification: Singleton can be recognized by a static creational method, which returns same cached object.

Singleton in Other Languages

Singleton in Java Singleton in C# Singleton in PHP Singleton in Python Singleton in Swift Singleton in TypeScript