title: Resetting a singleton instance in Ruby
body: <p>How do I reset a singleton object in Ruby? I know that one'd never want to do this in <em>real</em> code but what about unit tests?</p>
  
  <p>Here's what I am trying to do in an RSpec test -</p>
  
  <pre><code>describe MySingleton, "#not_initialised" do
    it "raises an exception" do
      expect {MySingleton.get_something}.to raise_error(RuntimeError)
    end
  end
  </code></pre>
  
  <p>It fails because one of my previous tests initialises the singleton object. I have tried following Ian White's advice from <a href="http://blog.ardes.com/2006/12/11/testing-singletons-with-ruby" rel="noreferrer">this</a> link which essentially monkey patches Singleton to provide a reset_instance method but I get an undefined method 'reset_instance' exception.</p>
  
  <pre><code>require 'singleton'
  
  class &lt;&lt;Singleton
    def included_with_reset(klass)
      included_without_reset(klass)
      class &lt;&lt;klass
        def reset_instance
          Singleton.send :__init__, self
          self
        end
      end
    end
    alias_method :included_without_reset, :included
    alias_method :included, :included_with_reset
  end
  
  describe MySingleton, "#not_initialised" do
    it "raises an exception" do
      MySingleton.reset_instance
      expect {MySingleton.get_something}.to raise_error(RuntimeError)
    end
  end
  </code></pre>
  
  <p>What is the most idiomatic way to do this in Ruby?</p>