summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/delegate.rb18
-rw-r--r--lib/weakref.rb13
2 files changed, 20 insertions, 11 deletions
diff --git a/lib/delegate.rb b/lib/delegate.rb
index bc6f876212..7889c8284f 100644
--- a/lib/delegate.rb
+++ b/lib/delegate.rb
@@ -21,7 +21,17 @@ class Delegator
preserved -= ["__getobj__","to_s","nil?","to_a","hash","dup","==","=~"]
for method in obj.methods
next if preserved.include? method
- eval "def self.#{method}(*args,&block); __getobj__.__send__(:#{method}, *args,&block); end"
+ eval <<EOS
+def self.#{method}(*args, &block)
+ begin
+ __getobj__.__send__(:#{method}, *args, &block)
+ rescue Exception
+ n = if /:in `__getobj__'$/ =~ $@[0] then 1 else 2 end #`
+ $@[1,n] = nil
+ raise
+ end
+end
+EOS
end
end
@@ -53,6 +63,10 @@ SimpleDelegater = SimpleDelegator
if __FILE__ == $0
foo = Object.new
+ def foo.test
+ raise 'this is OK'
+ end
foo2 = SimpleDelegator.new(foo)
- p foo.hash == foo2.hash # => true
+ p foo.hash == foo2.hash # => true
+ foo.test # raise error!
end
diff --git a/lib/weakref.rb b/lib/weakref.rb
index 6ef4b422c8..539bb9336c 100644
--- a/lib/weakref.rb
+++ b/lib/weakref.rb
@@ -41,14 +41,9 @@ class WeakRef<Delegator
def __getobj__
unless ID_MAP[@__id]
- $@ = caller(1)
- $! = RefError.new("Illegal Reference - probably recycled")
- raise
+ raise RefError, "Illegal Reference - probably recycled", caller(2)
end
ObjectSpace._id2ref(@__id)
-# ObjectSpace.each_object do |obj|
-# return obj if obj.id == @__id
-# end
end
def weakref_alive?
@@ -66,9 +61,9 @@ end
if __FILE__ == $0
foo = Object.new
- p foo.hash
+ p foo.hash # original's hash value
foo = WeakRef.new(foo)
- p foo.hash
+ p foo.hash # should be same hash value
ObjectSpace.garbage_collect
- p foo.hash
+ p foo.hash # should raise exception (recycled)
end