class Wref
A simple weak-reference framework with mapping. Only handles the referencing of objects.
Examples¶ ↑
user_obj = ob.get(:User, 1) weak_ref = Wref.new(user_obj) user_obj = nil sleep 0.5 GC.start begin user_obj = weak_ref.get print "The user still exists in memory and has ID #{user.id}." rescue Wref::Recycled print "The user has been removed from memory." end
Attributes
implementation[R]
weak_ref[R]
Public Class Methods
new(object, args = {})
click to toggle source
Initializes various variables.
# File lib/wref.rb, line 36 def initialize(object, args = {}) if args[:impl] @implementation = args[:impl] elsif RUBY_ENGINE == "jruby" @implementation = :JavaWeakReference else @implementation = :IdClassUnique end @weak_ref = Wref::Implementations.const_get(implementation).new(object) end
Public Instance Methods
alive?()
click to toggle source
Returns true if the reference is still alive.
print "The object still exists in memory." if wref.alive?
# File lib/wref.rb, line 66 def alive? @weak_ref.alive? end
Also aliased as: weakref_alive?
get()
click to toggle source
The same as the normal 'get!' but returns nil instead of raising Wref::Cycled-error.
# File lib/wref.rb, line 60 def get @weak_ref.get end
Also aliased as: __getobj__
get!()
click to toggle source
Returns the object that this weak reference holds or raises Wref::Recycled
.
begin object = wref.get! puts "Object still exists in memory." rescue Wref::Recycled puts "Object has been garbage-collected." end
# File lib/wref.rb, line 55 def get! @weak_ref.get! end