Skip to content

Commit eb50d2e

Browse files
& -> and
1 parent 66f6528 commit eb50d2e

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

posts/2025-07-04-breaking-the-singleton-how-to-reload-ruby-singleton-instance.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ A few days ago I was involved in migrating multiple client libraries that were u
1717

1818
The initial idea was simple - let's generate target URLs on production for every client that needs to be migrated and then write a test case that will change the Rails environment to return `production` in a test and will check whether client URLs are correct after changing the internal implementation. In theory this should work and indeed, it was working when I was running the test in isolation. But then I realised that when I run the whole test suite, the first test that is using an instance of my singleton class will set its state for the whole test suite. This means that my test will pass only if it's called before any other test that is using any of client instances and - even worse - it will return instances with production-like state for all test cases that are executed after my client test. I really needed to re-instantiate my singleton classes or I would need to run a separate test suite where I run only my single test for the migration.
1919

20-
I started from inspecting the Ruby singleton [sources](https://github.com/ruby/singleton/tree/master) and shortly thereafter I found the undocumented [`__init__`](https://github.com/ruby/singleton/blob/3f4e1f55f53eae16d3430761378697b3ebe5f1a4/lib/singleton.rb#L162-L168) method that does exactly what I needed - it resets the singleton class state by removing the instance (setting it to `nil`) and creating a new mutex for thread-safety. So the next time you call the `instance` method of your singleton class, it will create a new instance. Now I only needed to setup the stage in my test so that I stub the Rails env & reset singleton instance before my test and remember to remove the env stub & reset singleton instances again after the test is run:
20+
I started from inspecting the Ruby singleton [sources](https://github.com/ruby/singleton/tree/master) and shortly thereafter I found the undocumented [`__init__`](https://github.com/ruby/singleton/blob/3f4e1f55f53eae16d3430761378697b3ebe5f1a4/lib/singleton.rb#L162-L168) method that does exactly what I needed - it resets the singleton class state by removing the instance (setting it to `nil`) and creating a new mutex for thread-safety. So the next time you call the `instance` method of your singleton class, it will create a new instance. Now I only needed to setup the stage in my test so that I stub the Rails env and reset singleton instance before my test and remember to remove the env stub and reset singleton instances again after the test is run:
2121

2222
```ruby
2323
RSpec.describe "Clients migration" do

0 commit comments

Comments
 (0)