Module: Singleton

Defined in:
lib/singleton.rb

Defined Under Namespace

Modules: SingletonClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.__init__(klass) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/singleton.rb', line 100

def __init__(klass)
  klass.instance_eval {
    @singleton__instance__ = nil
    @singleton__mutex__ = Mutex.new
  }
  def klass.instance
    return @singleton__instance__ if @singleton__instance__
    @singleton__mutex__.synchronize {
      return @singleton__instance__ if @singleton__instance__
      @singleton__instance__ = new()
    }
    @singleton__instance__
  end
  klass
end

Instance Method Details

#_dump(depth = -1)) ⇒ Object

default marshalling strategy



75
76
77
# File 'lib/singleton.rb', line 75

def _dump(depth = -1)
  ''
end

#cloneObject

disable build-in copying methods

Raises:

  • (TypeError)


67
68
69
# File 'lib/singleton.rb', line 67

def clone
  raise TypeError, "can't clone instance of singleton #{self.class}"
end

#dupObject

Raises:

  • (TypeError)


70
71
72
# File 'lib/singleton.rb', line 70

def dup
  raise TypeError, "can't dup instance of singleton #{self.class}"
end