Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 61 lines (40 sloc) 1.339 kb
ea632b9 Charles Oliver Nutter Add README and remove the hash attr on WeakRef::IdHash.
authored
1 weakling: a collection of weakref utilities for Ruby
2
3 == Summary ==
4
5 This library provides:
6
7 * a modified WeakRef implementation for JRuby that supports a reference queue
8 * a WeakRef::RefQueue class
9 * a weak-valued ID map to replace typical uses of _id2ref
10
11 In the future it may provide additional features like a generic WeakHash or other
12 reference types like soft and phantom references.
13
300f4aa Charles Oliver Nutter Minor tweaks, added HISTORY, updated to 0.0.2
authored
14 The weakling repository is at http://github.com/headius/weakling.
15
ea632b9 Charles Oliver Nutter Add README and remove the hash attr on WeakRef::IdHash.
authored
16 == Usage ==
17
18 Just require 'weakling'. It will require 'weakref' along with the refqueue JRuby
19 extension and the weakling/collections library containing the weak id hash.
20
7cbaa2f Charles Oliver Nutter Add an example of using WeakRef::RefQueue
authored
21 == RefQueue Example ==
22
23 require 'weakling'
24 require 'java'
25
26 q = WeakRef::RefQueue.new
27 wr = WeakRef.new(Object.new, q)
28 puts "weakref object: #{wr.__id__}"
29
30 puts "running GC"
31 java.lang.System.gc
32
33 puts "weakref alive?: #{wr.weakref_alive?}"
34 puts "weakref object from queue: #{q.poll.__id__}"
35
36 == WeakRef::IdHash Example ==
ea632b9 Charles Oliver Nutter Add README and remove the hash attr on WeakRef::IdHash.
authored
37
38 require 'weakling'
39
40 wh = WeakRef::IdHash.new
41
42 ary = (1..10).to_a.map {Object.new}
43 ids = ary.map {|o| wh.add(o)}
44
45 puts "all items in weak_id_hash:"
46 ids.each {|i| puts "#{i} = #{wh[i]}"}
47
48 puts "dereferencing objects"
49 ary = nil
50
51 puts "forcing GC"
52 begin
53 require 'java'
54 java.lang.System.gc
55 rescue
56 GC.start
57 end
58
59 puts "all items in weak id hash:"
60 ids.each {|i| puts "#{i} = #{wh[i]}"}
Something went wrong with that request. Please try again.