summaryrefslogtreecommitdiff
path: root/lib/weakref.rb
diff options
context:
space:
mode:
authormatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>1999-01-20 04:59:39 +0000
committermatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>1999-01-20 04:59:39 +0000
commit210367ec889f5910e270d6ea2c7ddb8a8d939e61 (patch)
treefeb35473da45947378fbc02defe39bcd79ef600e /lib/weakref.rb
parent9c5b1986a36c7a700b4c76817e35aa874ba7907c (diff)
This commit was generated by cvs2svn to compensate for changes in r372,
which included commits to RCS files with non-trunk default branches. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@373 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/weakref.rb')
-rw-r--r--lib/weakref.rb39
1 files changed, 19 insertions, 20 deletions
diff --git a/lib/weakref.rb b/lib/weakref.rb
index 93b2c65..c31e959 100644
--- a/lib/weakref.rb
+++ b/lib/weakref.rb
@@ -10,9 +10,10 @@
require "delegate"
-class WeakRef<Delegater
+class WeakRef<Delegator
- Exception :RefError
+ class RefError<StandardError
+ end
ID_MAP = {}
ID_REV_MAP = {}
@@ -31,26 +32,22 @@ class WeakRef<Delegater
def initialize(orig)
super
- @id = orig.id
+ @__id = orig.__id__
ObjectSpace.call_finalizer orig
- ID_MAP[@id] = self.id
- ID_REV_MAP[self.id] = @id
+ ObjectSpace.call_finalizer self
+ ID_MAP[@__id] = self.__id__
+ ID_REV_MAP[self.id] = @__id
end
def __getobj__
- unless ID_MAP[@id]
- $@ = caller(1)
- $! = RefError.new("Illegal Reference - probably recycled")
- raise
+ unless ID_MAP[@__id]
+ raise RefError, "Illegal Reference - probably recycled", caller(2)
end
- ObjectSpace.id2ref(@id)
-# ObjectSpace.each_object do |obj|
-# return obj if obj.id == @id
-# end
+ ObjectSpace._id2ref(@__id)
end
def weakref_alive?
- if ID_MAP[@id]
+ if ID_MAP[@__id]
true
else
false
@@ -62,9 +59,11 @@ class WeakRef<Delegater
end
end
-foo = Object.new
-p foo.hash
-foo = WeakRef.new(foo)
-p foo.hash
-ObjectSpace.garbage_collect
-p foo.hash
+if __FILE__ == $0
+ foo = Object.new
+ p foo.hash # original's hash value
+ foo = WeakRef.new(foo)
+ p foo.hash # should be same hash value
+ ObjectSpace.garbage_collect
+ p foo.hash # should raise exception (recycled)
+end