summaryrefslogtreecommitdiff
path: root/lib/singleton.rb
diff options
context:
space:
mode:
authormatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2001-12-03 10:07:48 +0000
committermatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2001-12-03 10:07:48 +0000
commitb3144a7a0e08500d212c9f30e889dfcff631fbc4 (patch)
treef07f5ab290541a34d6ce978bc4961917c1c6ac54 /lib/singleton.rb
parent9e8553e5cccd1507597b7726da15a13fcae192af (diff)
* time.c (time_plus): must detect result overflow.
* time.c (time_minus): ditto. * time.c (time_new_internal): round usec overflow and underflow here. * time.c (time_plus): move operand overflow/underflow check to time_new_internal(). * time.c (time_minus): ditto. * time.c (time_cmp): should consider tv_usec too. * time.c (time_gmtime): time_modify() should be called even if tm struct is not calculated yet. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@1880 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/singleton.rb')
-rw-r--r--lib/singleton.rb110
1 files changed, 57 insertions, 53 deletions
diff --git a/lib/singleton.rb b/lib/singleton.rb
index 3ab048f5a3..404eaf7101 100644
--- a/lib/singleton.rb
+++ b/lib/singleton.rb
@@ -44,60 +44,64 @@
# of a previous state of ``the instance'' - see third example.
#
module Singleton
- def Singleton.included (klass)
- # should this be checked?
- # raise TypeError.new "..." if klass.type == Module
- class << klass
- def inherited(sub_klass)
- # @__instance__ takes on one of the following values
- # * nil - before (and after a failed) creation
- # * false - during creation
- # * sub_class instance - after a successful creation
- sub_klass.instance_eval { @__instance__ = nil }
- def sub_klass.instance
- unless @__instance__.nil?
- # is the extra flexiblity having the hook method
- # _wait() around ever useful?
- _wait()
- # check for instance creation
- return @__instance__ if @__instance__
- end
- Thread.critical = true
- unless @__instance__
- @__instance__ = false
- Thread.critical = false
- begin
- @__instance__ = new
- ensure
- if @__instance__
- define_method(:instance) {@__instance__ }
- else
- # failed instance creation
- @__instance__ = nil
- end
- end
- else
- Thread.critical = false
- end
- return @__instance__
- end
- end
- def _load(str)
- instance
- end
- def _wait
- sleep(0.05) while false.equal?(@__instance__)
- end
- private :new, :allocate
- # hook methods are also marked private
- private :_load,:_wait
- end
- klass.inherited klass
- end
- private
- def _dump(depth)
- return ""
+ def Singleton.included (klass)
+ # should this be checked?
+ # raise TypeError.new "..." if klass.type == Module
+ klass.module_eval {
+ undef_method :clone
+ undef_method :dup
+ }
+ class << klass
+ def inherited(sub_klass)
+ # @__instance__ takes on one of the following values
+ # * nil - before (and after a failed) creation
+ # * false - during creation
+ # * sub_class instance - after a successful creation
+ sub_klass.instance_eval { @__instance__ = nil }
+ def sub_klass.instance
+ unless @__instance__.nil?
+ # is the extra flexiblity having the hook method
+ # _wait() around ever useful?
+ _wait()
+ # check for instance creation
+ return @__instance__ if @__instance__
+ end
+ Thread.critical = true
+ unless @__instance__
+ @__instance__ = false
+ Thread.critical = false
+ begin
+ @__instance__ = new
+ ensure
+ if @__instance__
+ define_method(:instance) {@__instance__ }
+ else
+ # failed instance creation
+ @__instance__ = nil
+ end
+ end
+ else
+ Thread.critical = false
+ end
+ return @__instance__
+ end
+ end
+ def _load(str)
+ instance
+ end
+ def _wait
+ sleep(0.05) while false.equal?(@__instance__)
+ end
+ private :new, :allocate
+ # hook methods are also marked private
+ private :_load,:_wait
end
+ klass.inherited klass
+ end
+ private
+ def _dump(depth)
+ return ""
+ end
end
if __FILE__ == $0