This might be premature optimization, or premature over-cautionization, but I’m avoiding using singletons on a few classes because I’m concerned that down the road I’ll need to run my app as multi-threaded, and that the singletons will create conflicts and messiness. Do singletons have this issue in Ruby, or is there some kind of built in namespace so that when an a class refers to the singleton, only the singleton on the same thread is returned?
Edit: to clarify these are observable classes which when updated cause the other classes that are watching them to update. I’m not sure if this is thread safe or not, but I do know that right now I’m passing these observable classes around a ton and it’s kind of annoying. And they do seem like natural singleton classes.
All classes that aren’t written to be thread-safe will cause problems in a multi-threaded environment, regardless of whether they’re singletons or not.
The fact that your class is a singleton could make the problem worse, because it’s shared by default. You can’t have an instance per-thread.
If the singleton state is read-only and immutable you won’t have a thread safety issue.
If shared state is modified, you have to ensure that it’s thread safe.