ruby-changes:20266
From: drbrain <ko1@a...>
Date: Thu, 30 Jun 2011 09:37:05 +0900 (JST)
Subject: [ruby-changes:20266] drbrain:r32314 (trunk): * lib/weakref.rb: Attach documentation to WeakRef and add missing
drbrain 2011-06-30 09:37:00 +0900 (Thu, 30 Jun 2011) New Revision: 32314 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=32314 Log: * lib/weakref.rb: Attach documentation to WeakRef and add missing documentation Modified files: trunk/ChangeLog trunk/lib/weakref.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 32313) +++ ChangeLog (revision 32314) @@ -1,3 +1,8 @@ +Thu Jun 30 09:36:37 2011 Eric Hodel <drbrain@s...> + + * lib/weakref.rb: Attach documentation to WeakRef and add missing + documentation + Thu Jun 30 09:30:14 2011 Eric Hodel <drbrain@s...> * lib/yaml.rb: Document toplevel YAML and YAML::ENGINE to describe Index: lib/weakref.rb =================================================================== --- lib/weakref.rb (revision 32313) +++ lib/weakref.rb (revision 32314) @@ -1,6 +1,12 @@ -# Weak Reference class that does not bother GCing. +require "delegate" +require 'thread' + +# Weak Reference class that does allows a referenced object to be +# garbage-collected. A WeakRef may be used exactly like the object it +# references. # # Usage: +# # foo = Object.new # foo = Object.new # p foo.to_s # original's class @@ -9,11 +15,12 @@ # ObjectSpace.garbage_collect # p foo.to_s # should raise exception (recycled) -require "delegate" -require 'thread' - class WeakRef < Delegator + ## + # RefError is raised when a referenced object has been recycled by the + # garbage collector + class RefError < StandardError end @@ -38,6 +45,9 @@ } } + ## + # Creates a weak reference to +orig+ + def initialize(orig) @__id = orig.object_id ObjectSpace.define_finalizer orig, @@final @@ -50,7 +60,7 @@ super end - def __getobj__ + def __getobj__ # :nodoc: unless @@id_rev_map[self.object_id] == @__id Kernel::raise RefError, "Invalid Reference - probably recycled", Kernel::caller(2) end @@ -60,9 +70,13 @@ Kernel::raise RefError, "Invalid Reference - probably recycled", Kernel::caller(2) end end - def __setobj__(obj) + + def __setobj__(obj) # :nodoc: end + ## + # Returns true if the referenced object is still alive. + def weakref_alive? @@id_rev_map[self.object_id] == @__id end -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/