Ruby's Singleton module ensures only one instance of a class exists throughout an application's lifecycle. However, testing singleton classes can be problematic as their state persists across test cases. The undocumented `Singleton.__init__` method can reset a singleton's state by removing the existing instance and creating a new mutex, allowing fresh instantiation. This technique is particularly useful when testing environment-specific configurations or migrating singleton-based implementations. The article also covers the difference between `Singleton.__init__` (which resets existing instances) and `clone` (which creates new anonymous singleton classes).
Sort: