Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: db9f1ede13
Fetching contributors…

Cannot retrieve contributors at this time

executable file 43 lines (28 sloc) 0.671 kb
#!/usr/bin/env ruby
require 'test/unit'
class SingletonTest < Test::Unit::TestCase
def test_custom_singleton
a = MySingleton.create
b = MySingleton.create
assert_equal(a.object_id, b.object_id)
end
def test_mixin_singleton
a = MySingletonMixin.instance
b = MySingletonMixin.instance
assert_equal(a.object_id, b.object_id)
end
end
# Not thread-safe!
class MySingleton
private_class_method :new
@@instance = nil
def MySingleton.create
@@instance = new unless @@instance
@@instance
end
end
# A thread-safe alternative to a custom singleton.
require 'singleton'
class MySingletonMixin
include Singleton
end
Jump to Line
Something went wrong with that request. Please try again.