mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* lib/singleton.rb (SingletonClassMethods): _load should be public.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@16659 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
e0029988fb
commit
4f4ab5e230
2 changed files with 35 additions and 31 deletions
|
@ -30,7 +30,7 @@
|
|||
# * Klass.new and Klass.allocate - as private
|
||||
#
|
||||
# Providing (or modifying) the class methods
|
||||
# * Klass.inherited(sub_klass) and Klass.clone() -
|
||||
# * Klass.inherited(sub_klass) and Klass.clone() -
|
||||
# to ensure that the Singleton pattern is properly
|
||||
# inherited and cloned.
|
||||
#
|
||||
|
@ -70,10 +70,10 @@ module Singleton
|
|||
def dup
|
||||
raise TypeError, "can't dup instance of singleton #{self.class}"
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
private
|
||||
# default marshalling strategy
|
||||
def _dump(depth=-1)
|
||||
def _dump(depth=-1)
|
||||
''
|
||||
end
|
||||
end
|
||||
|
@ -104,7 +104,7 @@ class << Singleton
|
|||
end
|
||||
end
|
||||
elsif _instantiate?()
|
||||
Thread.critical = false
|
||||
Thread.critical = false
|
||||
else
|
||||
@__instance__ = false
|
||||
Thread.critical = false
|
||||
|
@ -123,26 +123,26 @@ class << Singleton
|
|||
end
|
||||
@__instance__
|
||||
end
|
||||
|
||||
module SingletonClassMethods
|
||||
|
||||
module SingletonClassMethods
|
||||
# properly clone the Singleton pattern - did you know
|
||||
# that duping doesn't copy class methods?
|
||||
# that duping doesn't copy class methods?
|
||||
def clone
|
||||
Singleton.__init__(super)
|
||||
end
|
||||
|
||||
|
||||
def _load(str)
|
||||
instance
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# ensure that the Singleton pattern is properly inherited
|
||||
|
||||
# ensure that the Singleton pattern is properly inherited
|
||||
def inherited(sub_klass)
|
||||
super
|
||||
Singleton.__init__(sub_klass)
|
||||
end
|
||||
|
||||
def _load(str)
|
||||
instance
|
||||
end
|
||||
|
||||
|
||||
# waiting-loop hook
|
||||
def _instantiate?()
|
||||
while false.equal?(@__instance__)
|
||||
|
@ -153,7 +153,7 @@ class << Singleton
|
|||
@__instance__
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
def __init__(klass)
|
||||
klass.instance_eval { @__instance__ = nil }
|
||||
class << klass
|
||||
|
@ -161,11 +161,11 @@ class << Singleton
|
|||
end
|
||||
klass
|
||||
end
|
||||
|
||||
|
||||
private
|
||||
# extending an object with Singleton is a bad idea
|
||||
undef_method :extend_object
|
||||
|
||||
|
||||
def append_features(mod)
|
||||
# help out people counting on transitive mixins
|
||||
unless mod.instance_of?(Class)
|
||||
|
@ -173,7 +173,7 @@ class << Singleton
|
|||
end
|
||||
super
|
||||
end
|
||||
|
||||
|
||||
def included(klass)
|
||||
super
|
||||
klass.private_class_method :new, :allocate
|
||||
|
@ -181,21 +181,21 @@ class << Singleton
|
|||
Singleton.__init__(klass)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
||||
|
||||
if __FILE__ == $0
|
||||
|
||||
def num_of_instances(klass)
|
||||
"#{ObjectSpace.each_object(klass){}} #{klass} instance(s)"
|
||||
end
|
||||
end
|
||||
|
||||
# The basic and most important example.
|
||||
|
||||
class SomeSingletonClass
|
||||
include Singleton
|
||||
end
|
||||
puts "There are #{num_of_instances(SomeSingletonClass)}"
|
||||
puts "There are #{num_of_instances(SomeSingletonClass)}"
|
||||
|
||||
a = SomeSingletonClass.instance
|
||||
b = SomeSingletonClass.instance # a and b are same object
|
||||
|
@ -218,23 +218,23 @@ class Ups < SomeSingletonClass
|
|||
puts "initialize called by thread ##{Thread.current[:i]}"
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
class << Ups
|
||||
def _instantiate?
|
||||
@enter.push Thread.current[:i]
|
||||
while false.equal?(@__instance__)
|
||||
Thread.critical = false
|
||||
sleep 0.08
|
||||
sleep 0.08
|
||||
Thread.critical = true
|
||||
end
|
||||
@leave.push Thread.current[:i]
|
||||
@__instance__
|
||||
end
|
||||
|
||||
|
||||
def __sleep
|
||||
sleep(rand(0.08))
|
||||
end
|
||||
|
||||
|
||||
def new
|
||||
begin
|
||||
__sleep
|
||||
|
@ -246,12 +246,12 @@ class << Ups
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
def instantiate_all
|
||||
@enter = []
|
||||
@leave = []
|
||||
1.upto(9) {|i|
|
||||
Thread.new {
|
||||
1.upto(9) {|i|
|
||||
Thread.new {
|
||||
begin
|
||||
Thread.current[:i] = i
|
||||
__sleep
|
||||
|
@ -341,7 +341,7 @@ end
|
|||
class Down < Middle; end
|
||||
|
||||
puts "and basic \"Down test\" is #{Down.instance == Down.instance}\n
|
||||
Various exceptions"
|
||||
Various exceptions"
|
||||
|
||||
begin
|
||||
module AModule
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue