* lib/singleton.rb (SingletonClassMethods): _load should be public.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@16659 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
3ab17047f5
commit
e02270d85d
@ -1,3 +1,7 @@
|
|||||||
|
Wed May 28 15:18:16 2008 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||||
|
|
||||||
|
* lib/singleton.rb (SingletonClassMethods): _load should be public.
|
||||||
|
|
||||||
Wed May 28 13:30:43 2008 NARUSE, Yui <naruse@ruby-lang.org>
|
Wed May 28 13:30:43 2008 NARUSE, Yui <naruse@ruby-lang.org>
|
||||||
|
|
||||||
* enc/trans/japanese.c: add workarround for Unicode to CP932.
|
* enc/trans/japanese.c: add workarround for Unicode to CP932.
|
||||||
|
@ -30,7 +30,7 @@
|
|||||||
# * Klass.new and Klass.allocate - as private
|
# * Klass.new and Klass.allocate - as private
|
||||||
#
|
#
|
||||||
# Providing (or modifying) the class methods
|
# 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
|
# to ensure that the Singleton pattern is properly
|
||||||
# inherited and cloned.
|
# inherited and cloned.
|
||||||
#
|
#
|
||||||
@ -70,32 +70,32 @@ module Singleton
|
|||||||
def dup
|
def dup
|
||||||
raise TypeError, "can't dup instance of singleton #{self.class}"
|
raise TypeError, "can't dup instance of singleton #{self.class}"
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
# default marshalling strategy
|
# default marshalling strategy
|
||||||
def _dump(depth = -1)
|
def _dump(depth = -1)
|
||||||
''
|
''
|
||||||
end
|
end
|
||||||
|
|
||||||
module SingletonClassMethods
|
module SingletonClassMethods
|
||||||
# properly clone the Singleton pattern - did you know
|
# properly clone the Singleton pattern - did you know
|
||||||
# that duping doesn't copy class methods?
|
# that duping doesn't copy class methods?
|
||||||
def clone
|
def clone
|
||||||
Singleton.__init__(super)
|
Singleton.__init__(super)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def _load(str)
|
||||||
|
instance
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
# ensure that the Singleton pattern is properly inherited
|
# ensure that the Singleton pattern is properly inherited
|
||||||
def inherited(sub_klass)
|
def inherited(sub_klass)
|
||||||
super
|
super
|
||||||
Singleton.__init__(sub_klass)
|
Singleton.__init__(sub_klass)
|
||||||
end
|
end
|
||||||
|
|
||||||
def _load(str)
|
|
||||||
instance
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
class << Singleton
|
class << Singleton
|
||||||
@ -114,12 +114,12 @@ module Singleton
|
|||||||
end
|
end
|
||||||
klass
|
klass
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
# extending an object with Singleton is a bad idea
|
# extending an object with Singleton is a bad idea
|
||||||
undef_method :extend_object
|
undef_method :extend_object
|
||||||
|
|
||||||
def append_features(mod)
|
def append_features(mod)
|
||||||
# help out people counting on transitive mixins
|
# help out people counting on transitive mixins
|
||||||
unless mod.instance_of?(Class)
|
unless mod.instance_of?(Class)
|
||||||
@ -127,7 +127,7 @@ module Singleton
|
|||||||
end
|
end
|
||||||
super
|
super
|
||||||
end
|
end
|
||||||
|
|
||||||
def included(klass)
|
def included(klass)
|
||||||
super
|
super
|
||||||
klass.private_class_method :new, :allocate
|
klass.private_class_method :new, :allocate
|
||||||
@ -135,7 +135,7 @@ module Singleton
|
|||||||
Singleton.__init__(klass)
|
Singleton.__init__(klass)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
@ -143,14 +143,14 @@ if __FILE__ == $0
|
|||||||
|
|
||||||
def num_of_instances(klass)
|
def num_of_instances(klass)
|
||||||
"#{ObjectSpace.each_object(klass){}} #{klass} instance(s)"
|
"#{ObjectSpace.each_object(klass){}} #{klass} instance(s)"
|
||||||
end
|
end
|
||||||
|
|
||||||
# The basic and most important example.
|
# The basic and most important example.
|
||||||
|
|
||||||
class SomeSingletonClass
|
class SomeSingletonClass
|
||||||
include Singleton
|
include Singleton
|
||||||
end
|
end
|
||||||
puts "There are #{num_of_instances(SomeSingletonClass)}"
|
puts "There are #{num_of_instances(SomeSingletonClass)}"
|
||||||
|
|
||||||
a = SomeSingletonClass.instance
|
a = SomeSingletonClass.instance
|
||||||
b = SomeSingletonClass.instance # a and b are same object
|
b = SomeSingletonClass.instance # a and b are same object
|
||||||
@ -173,23 +173,23 @@ class Ups < SomeSingletonClass
|
|||||||
puts "initialize called by thread ##{Thread.current[:i]}"
|
puts "initialize called by thread ##{Thread.current[:i]}"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class << Ups
|
class << Ups
|
||||||
def _instantiate?
|
def _instantiate?
|
||||||
@enter.push Thread.current[:i]
|
@enter.push Thread.current[:i]
|
||||||
while false.equal?(@singleton__instance__)
|
while false.equal?(@singleton__instance__)
|
||||||
@singleton__mutex__.unlock
|
@singleton__mutex__.unlock
|
||||||
sleep 0.08
|
sleep 0.08
|
||||||
@singleton__mutex__.lock
|
@singleton__mutex__.lock
|
||||||
end
|
end
|
||||||
@leave.push Thread.current[:i]
|
@leave.push Thread.current[:i]
|
||||||
@singleton__instance__
|
@singleton__instance__
|
||||||
end
|
end
|
||||||
|
|
||||||
def __sleep
|
def __sleep
|
||||||
sleep(rand(0.08))
|
sleep(rand(0.08))
|
||||||
end
|
end
|
||||||
|
|
||||||
def new
|
def new
|
||||||
begin
|
begin
|
||||||
__sleep
|
__sleep
|
||||||
@ -201,12 +201,12 @@ class << Ups
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def instantiate_all
|
def instantiate_all
|
||||||
@enter = []
|
@enter = []
|
||||||
@leave = []
|
@leave = []
|
||||||
1.upto(9) {|i|
|
1.upto(9) {|i|
|
||||||
Thread.new {
|
Thread.new {
|
||||||
begin
|
begin
|
||||||
Thread.current[:i] = i
|
Thread.current[:i] = i
|
||||||
__sleep
|
__sleep
|
||||||
@ -296,7 +296,7 @@ end
|
|||||||
class Down < Middle; end
|
class Down < Middle; end
|
||||||
|
|
||||||
puts "and basic \"Down test\" is #{Down.instance == Down.instance}\n
|
puts "and basic \"Down test\" is #{Down.instance == Down.instance}\n
|
||||||
Various exceptions"
|
Various exceptions"
|
||||||
|
|
||||||
begin
|
begin
|
||||||
module AModule
|
module AModule
|
||||||
|
Loading…
x
Reference in New Issue
Block a user