summaryrefslogtreecommitdiff
path: root/lib/weakref.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/weakref.rb')
-rw-r--r--lib/weakref.rb31
1 files changed, 12 insertions, 19 deletions
diff --git a/lib/weakref.rb b/lib/weakref.rb
index 78f88a3f12..c7274f9664 100644
--- a/lib/weakref.rb
+++ b/lib/weakref.rb
@@ -1,20 +1,24 @@
+# frozen_string_literal: true
require "delegate"
# Weak Reference class that allows a referenced object to be
-# garbage-collected. A WeakRef may be used exactly like the object it
-# references.
+# garbage-collected.
+#
+# A WeakRef may be used exactly like the object it references.
#
# Usage:
#
-# foo = Object.new
-# foo = Object.new
+# foo = Object.new # create a new object instance
# p foo.to_s # original's class
-# foo = WeakRef.new(foo)
+# foo = WeakRef.new(foo) # reassign foo with WeakRef instance
# p foo.to_s # should be same class
-# ObjectSpace.garbage_collect
+# GC.start # start the garbage collector
# p foo.to_s # should raise exception (recycled)
+#
class WeakRef < Delegator
+ # The version string
+ VERSION = "0.1.4"
##
# RefError is raised when a referenced object has been recycled by the
@@ -38,7 +42,7 @@ class WeakRef < Delegator
super
end
- def __getobj__ # :nodoc:
+ def __getobj__(&_block) # :nodoc:
@@__map[self] or defined?(@delegate_sd_obj) ? @delegate_sd_obj :
Kernel::raise(RefError, "Invalid Reference - probably recycled", Kernel::caller(2))
end
@@ -50,17 +54,6 @@ class WeakRef < Delegator
# Returns true if the referenced object is still alive.
def weakref_alive?
- !!(@@__map[self] or defined?(@delegate_sd_obj))
+ @@__map.key?(self) or defined?(@delegate_sd_obj)
end
end
-
-if __FILE__ == $0
-# require 'thread'
- foo = Object.new
- p foo.to_s # original's class
- foo = WeakRef.new(foo)
- p foo.to_s # should be same class
- ObjectSpace.garbage_collect
- ObjectSpace.garbage_collect
- p foo.to_s # should raise exception (recycled)
-end