Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../opt/alt/ruby18/lib64/ruby/1.8
File: singleton.rb
# The Singleton module implements the Singleton pattern.
[0] Fix | Delete
#
[1] Fix | Delete
# Usage:
[2] Fix | Delete
# class Klass
[3] Fix | Delete
# include Singleton
[4] Fix | Delete
# # ...
[5] Fix | Delete
# end
[6] Fix | Delete
#
[7] Fix | Delete
# * this ensures that only one instance of Klass lets call it
[8] Fix | Delete
# ``the instance'' can be created.
[9] Fix | Delete
#
[10] Fix | Delete
# a,b = Klass.instance, Klass.instance
[11] Fix | Delete
# a == b # => true
[12] Fix | Delete
# a.new # NoMethodError - new is private ...
[13] Fix | Delete
#
[14] Fix | Delete
# * ``The instance'' is created at instantiation time, in other
[15] Fix | Delete
# words the first call of Klass.instance(), thus
[16] Fix | Delete
#
[17] Fix | Delete
# class OtherKlass
[18] Fix | Delete
# include Singleton
[19] Fix | Delete
# # ...
[20] Fix | Delete
# end
[21] Fix | Delete
# ObjectSpace.each_object(OtherKlass){} # => 0.
[22] Fix | Delete
#
[23] Fix | Delete
# * This behavior is preserved under inheritance and cloning.
[24] Fix | Delete
#
[25] Fix | Delete
#
[26] Fix | Delete
#
[27] Fix | Delete
# This is achieved by marking
[28] Fix | Delete
# * Klass.new and Klass.allocate - as private
[29] Fix | Delete
#
[30] Fix | Delete
# Providing (or modifying) the class methods
[31] Fix | Delete
# * Klass.inherited(sub_klass) and Klass.clone() -
[32] Fix | Delete
# to ensure that the Singleton pattern is properly
[33] Fix | Delete
# inherited and cloned.
[34] Fix | Delete
#
[35] Fix | Delete
# * Klass.instance() - returning ``the instance''. After a
[36] Fix | Delete
# successful self modifying (normally the first) call the
[37] Fix | Delete
# method body is a simple:
[38] Fix | Delete
#
[39] Fix | Delete
# def Klass.instance()
[40] Fix | Delete
# return @__instance__
[41] Fix | Delete
# end
[42] Fix | Delete
#
[43] Fix | Delete
# * Klass._load(str) - calling Klass.instance()
[44] Fix | Delete
#
[45] Fix | Delete
# * Klass._instantiate?() - returning ``the instance'' or
[46] Fix | Delete
# nil. This hook method puts a second (or nth) thread calling
[47] Fix | Delete
# Klass.instance() on a waiting loop. The return value
[48] Fix | Delete
# signifies the successful completion or premature termination
[49] Fix | Delete
# of the first, or more generally, current "instantiation thread".
[50] Fix | Delete
#
[51] Fix | Delete
#
[52] Fix | Delete
# The instance method of Singleton are
[53] Fix | Delete
# * clone and dup - raising TypeErrors to prevent cloning or duping
[54] Fix | Delete
#
[55] Fix | Delete
# * _dump(depth) - returning the empty string. Marshalling strips
[56] Fix | Delete
# by default all state information, e.g. instance variables and
[57] Fix | Delete
# taint state, from ``the instance''. Providing custom _load(str)
[58] Fix | Delete
# and _dump(depth) hooks allows the (partially) resurrections of
[59] Fix | Delete
# a previous state of ``the instance''.
[60] Fix | Delete
[61] Fix | Delete
[62] Fix | Delete
[63] Fix | Delete
module Singleton
[64] Fix | Delete
# disable build-in copying methods
[65] Fix | Delete
def clone
[66] Fix | Delete
raise TypeError, "can't clone instance of singleton #{self.class}"
[67] Fix | Delete
end
[68] Fix | Delete
def dup
[69] Fix | Delete
raise TypeError, "can't dup instance of singleton #{self.class}"
[70] Fix | Delete
end
[71] Fix | Delete
[72] Fix | Delete
# default marshalling strategy
[73] Fix | Delete
def _dump(depth=-1)
[74] Fix | Delete
''
[75] Fix | Delete
end
[76] Fix | Delete
end
[77] Fix | Delete
[78] Fix | Delete
[79] Fix | Delete
class << Singleton
[80] Fix | Delete
# Method body of first instance call.
[81] Fix | Delete
FirstInstanceCall = proc do
[82] Fix | Delete
# @__instance__ takes on one of the following values
[83] Fix | Delete
# * nil - before and after a failed creation
[84] Fix | Delete
# * false - during creation
[85] Fix | Delete
# * sub_class instance - after a successful creation
[86] Fix | Delete
# the form makes up for the lack of returns in progs
[87] Fix | Delete
Thread.critical = true
[88] Fix | Delete
if @__instance__.nil?
[89] Fix | Delete
@__instance__ = false
[90] Fix | Delete
Thread.critical = false
[91] Fix | Delete
begin
[92] Fix | Delete
@__instance__ = new
[93] Fix | Delete
ensure
[94] Fix | Delete
if @__instance__
[95] Fix | Delete
class <<self
[96] Fix | Delete
remove_method :instance
[97] Fix | Delete
def instance; @__instance__ end
[98] Fix | Delete
end
[99] Fix | Delete
else
[100] Fix | Delete
@__instance__ = nil # failed instance creation
[101] Fix | Delete
end
[102] Fix | Delete
end
[103] Fix | Delete
elsif _instantiate?()
[104] Fix | Delete
Thread.critical = false
[105] Fix | Delete
else
[106] Fix | Delete
@__instance__ = false
[107] Fix | Delete
Thread.critical = false
[108] Fix | Delete
begin
[109] Fix | Delete
@__instance__ = new
[110] Fix | Delete
ensure
[111] Fix | Delete
if @__instance__
[112] Fix | Delete
class <<self
[113] Fix | Delete
remove_method :instance
[114] Fix | Delete
def instance; @__instance__ end
[115] Fix | Delete
end
[116] Fix | Delete
else
[117] Fix | Delete
@__instance__ = nil
[118] Fix | Delete
end
[119] Fix | Delete
end
[120] Fix | Delete
end
[121] Fix | Delete
@__instance__
[122] Fix | Delete
end
[123] Fix | Delete
[124] Fix | Delete
module SingletonClassMethods
[125] Fix | Delete
# properly clone the Singleton pattern - did you know
[126] Fix | Delete
# that duping doesn't copy class methods?
[127] Fix | Delete
def clone
[128] Fix | Delete
Singleton.__init__(super)
[129] Fix | Delete
end
[130] Fix | Delete
[131] Fix | Delete
def _load(str)
[132] Fix | Delete
instance
[133] Fix | Delete
end
[134] Fix | Delete
[135] Fix | Delete
private
[136] Fix | Delete
[137] Fix | Delete
# ensure that the Singleton pattern is properly inherited
[138] Fix | Delete
def inherited(sub_klass)
[139] Fix | Delete
super
[140] Fix | Delete
Singleton.__init__(sub_klass)
[141] Fix | Delete
end
[142] Fix | Delete
[143] Fix | Delete
# waiting-loop hook
[144] Fix | Delete
def _instantiate?()
[145] Fix | Delete
while false.equal?(@__instance__)
[146] Fix | Delete
Thread.critical = false
[147] Fix | Delete
sleep(0.08) # timeout
[148] Fix | Delete
Thread.critical = true
[149] Fix | Delete
end
[150] Fix | Delete
@__instance__
[151] Fix | Delete
end
[152] Fix | Delete
end
[153] Fix | Delete
[154] Fix | Delete
def __init__(klass)
[155] Fix | Delete
klass.instance_eval { @__instance__ = nil }
[156] Fix | Delete
class << klass
[157] Fix | Delete
define_method(:instance,FirstInstanceCall)
[158] Fix | Delete
end
[159] Fix | Delete
klass
[160] Fix | Delete
end
[161] Fix | Delete
[162] Fix | Delete
private
[163] Fix | Delete
# extending an object with Singleton is a bad idea
[164] Fix | Delete
undef_method :extend_object
[165] Fix | Delete
[166] Fix | Delete
def append_features(mod)
[167] Fix | Delete
# help out people counting on transitive mixins
[168] Fix | Delete
unless mod.instance_of?(Class)
[169] Fix | Delete
raise TypeError, "Inclusion of the OO-Singleton module in module #{mod}"
[170] Fix | Delete
end
[171] Fix | Delete
super
[172] Fix | Delete
end
[173] Fix | Delete
[174] Fix | Delete
def included(klass)
[175] Fix | Delete
super
[176] Fix | Delete
klass.private_class_method :new, :allocate
[177] Fix | Delete
klass.extend SingletonClassMethods
[178] Fix | Delete
Singleton.__init__(klass)
[179] Fix | Delete
end
[180] Fix | Delete
end
[181] Fix | Delete
[182] Fix | Delete
[183] Fix | Delete
[184] Fix | Delete
if __FILE__ == $0
[185] Fix | Delete
[186] Fix | Delete
def num_of_instances(klass)
[187] Fix | Delete
"#{ObjectSpace.each_object(klass){}} #{klass} instance(s)"
[188] Fix | Delete
end
[189] Fix | Delete
[190] Fix | Delete
# The basic and most important example.
[191] Fix | Delete
[192] Fix | Delete
class SomeSingletonClass
[193] Fix | Delete
include Singleton
[194] Fix | Delete
end
[195] Fix | Delete
puts "There are #{num_of_instances(SomeSingletonClass)}"
[196] Fix | Delete
[197] Fix | Delete
a = SomeSingletonClass.instance
[198] Fix | Delete
b = SomeSingletonClass.instance # a and b are same object
[199] Fix | Delete
puts "basic test is #{a == b}"
[200] Fix | Delete
[201] Fix | Delete
begin
[202] Fix | Delete
SomeSingletonClass.new
[203] Fix | Delete
rescue NoMethodError => mes
[204] Fix | Delete
puts mes
[205] Fix | Delete
end
[206] Fix | Delete
[207] Fix | Delete
[208] Fix | Delete
[209] Fix | Delete
puts "\nThreaded example with exception and customized #_instantiate?() hook"; p
[210] Fix | Delete
Thread.abort_on_exception = false
[211] Fix | Delete
[212] Fix | Delete
class Ups < SomeSingletonClass
[213] Fix | Delete
def initialize
[214] Fix | Delete
self.class.__sleep
[215] Fix | Delete
puts "initialize called by thread ##{Thread.current[:i]}"
[216] Fix | Delete
end
[217] Fix | Delete
end
[218] Fix | Delete
[219] Fix | Delete
class << Ups
[220] Fix | Delete
def _instantiate?
[221] Fix | Delete
@enter.push Thread.current[:i]
[222] Fix | Delete
while false.equal?(@__instance__)
[223] Fix | Delete
Thread.critical = false
[224] Fix | Delete
sleep 0.08
[225] Fix | Delete
Thread.critical = true
[226] Fix | Delete
end
[227] Fix | Delete
@leave.push Thread.current[:i]
[228] Fix | Delete
@__instance__
[229] Fix | Delete
end
[230] Fix | Delete
[231] Fix | Delete
def __sleep
[232] Fix | Delete
sleep(rand(0.08))
[233] Fix | Delete
end
[234] Fix | Delete
[235] Fix | Delete
def new
[236] Fix | Delete
begin
[237] Fix | Delete
__sleep
[238] Fix | Delete
raise "boom - thread ##{Thread.current[:i]} failed to create instance"
[239] Fix | Delete
ensure
[240] Fix | Delete
# simple flip-flop
[241] Fix | Delete
class << self
[242] Fix | Delete
remove_method :new
[243] Fix | Delete
end
[244] Fix | Delete
end
[245] Fix | Delete
end
[246] Fix | Delete
[247] Fix | Delete
def instantiate_all
[248] Fix | Delete
@enter = []
[249] Fix | Delete
@leave = []
[250] Fix | Delete
1.upto(9) {|i|
[251] Fix | Delete
Thread.new {
[252] Fix | Delete
begin
[253] Fix | Delete
Thread.current[:i] = i
[254] Fix | Delete
__sleep
[255] Fix | Delete
instance
[256] Fix | Delete
rescue RuntimeError => mes
[257] Fix | Delete
puts mes
[258] Fix | Delete
end
[259] Fix | Delete
}
[260] Fix | Delete
}
[261] Fix | Delete
puts "Before there were #{num_of_instances(self)}"
[262] Fix | Delete
sleep 3
[263] Fix | Delete
puts "Now there is #{num_of_instances(self)}"
[264] Fix | Delete
puts "#{@enter.join '; '} was the order of threads entering the waiting loop"
[265] Fix | Delete
puts "#{@leave.join '; '} was the order of threads leaving the waiting loop"
[266] Fix | Delete
end
[267] Fix | Delete
end
[268] Fix | Delete
[269] Fix | Delete
[270] Fix | Delete
Ups.instantiate_all
[271] Fix | Delete
# results in message like
[272] Fix | Delete
# Before there were 0 Ups instance(s)
[273] Fix | Delete
# boom - thread #6 failed to create instance
[274] Fix | Delete
# initialize called by thread #3
[275] Fix | Delete
# Now there is 1 Ups instance(s)
[276] Fix | Delete
# 3; 2; 1; 8; 4; 7; 5 was the order of threads entering the waiting loop
[277] Fix | Delete
# 3; 2; 1; 7; 4; 8; 5 was the order of threads leaving the waiting loop
[278] Fix | Delete
[279] Fix | Delete
[280] Fix | Delete
puts "\nLets see if class level cloning really works"
[281] Fix | Delete
Yup = Ups.clone
[282] Fix | Delete
def Yup.new
[283] Fix | Delete
begin
[284] Fix | Delete
__sleep
[285] Fix | Delete
raise "boom - thread ##{Thread.current[:i]} failed to create instance"
[286] Fix | Delete
ensure
[287] Fix | Delete
# simple flip-flop
[288] Fix | Delete
class << self
[289] Fix | Delete
remove_method :new
[290] Fix | Delete
end
[291] Fix | Delete
end
[292] Fix | Delete
end
[293] Fix | Delete
Yup.instantiate_all
[294] Fix | Delete
[295] Fix | Delete
[296] Fix | Delete
puts "\n\n","Customized marshalling"
[297] Fix | Delete
class A
[298] Fix | Delete
include Singleton
[299] Fix | Delete
attr_accessor :persist, :die
[300] Fix | Delete
def _dump(depth)
[301] Fix | Delete
# this strips the @die information from the instance
[302] Fix | Delete
Marshal.dump(@persist,depth)
[303] Fix | Delete
end
[304] Fix | Delete
end
[305] Fix | Delete
[306] Fix | Delete
def A._load(str)
[307] Fix | Delete
instance.persist = Marshal.load(str)
[308] Fix | Delete
instance
[309] Fix | Delete
end
[310] Fix | Delete
[311] Fix | Delete
a = A.instance
[312] Fix | Delete
a.persist = ["persist"]
[313] Fix | Delete
a.die = "die"
[314] Fix | Delete
a.taint
[315] Fix | Delete
[316] Fix | Delete
stored_state = Marshal.dump(a)
[317] Fix | Delete
# change state
[318] Fix | Delete
a.persist = nil
[319] Fix | Delete
a.die = nil
[320] Fix | Delete
b = Marshal.load(stored_state)
[321] Fix | Delete
p a == b # => true
[322] Fix | Delete
p a.persist # => ["persist"]
[323] Fix | Delete
p a.die # => nil
[324] Fix | Delete
[325] Fix | Delete
[326] Fix | Delete
puts "\n\nSingleton with overridden default #inherited() hook"
[327] Fix | Delete
class Up
[328] Fix | Delete
end
[329] Fix | Delete
def Up.inherited(sub_klass)
[330] Fix | Delete
puts "#{sub_klass} subclasses #{self}"
[331] Fix | Delete
end
[332] Fix | Delete
[333] Fix | Delete
[334] Fix | Delete
class Middle < Up
[335] Fix | Delete
include Singleton
[336] Fix | Delete
end
[337] Fix | Delete
[338] Fix | Delete
class Down < Middle; end
[339] Fix | Delete
[340] Fix | Delete
puts "and basic \"Down test\" is #{Down.instance == Down.instance}\n
[341] Fix | Delete
Various exceptions"
[342] Fix | Delete
[343] Fix | Delete
begin
[344] Fix | Delete
module AModule
[345] Fix | Delete
include Singleton
[346] Fix | Delete
end
[347] Fix | Delete
rescue TypeError => mes
[348] Fix | Delete
puts mes #=> Inclusion of the OO-Singleton module in module AModule
[349] Fix | Delete
end
[350] Fix | Delete
[351] Fix | Delete
begin
[352] Fix | Delete
'aString'.extend Singleton
[353] Fix | Delete
rescue NoMethodError => mes
[354] Fix | Delete
puts mes #=> undefined method `extend_object' for Singleton:Module
[355] Fix | Delete
end
[356] Fix | Delete
[357] Fix | Delete
end
[358] Fix | Delete
[359] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function