# \[ANN\] weakling 0.0.1 - a reference queue and weak ID hash for JRuby

**URL:** <https://www.ruby-forum.com/t/ann-weakling-0-0-1-a-reference-queue-and-weak-id-hash-for-jruby/185031>\
**Category:** Ruby\
**Created:** [March 12, 2010, 2:49am UTC](https://www.ruby-forum.com/t/ann-weakling-0-0-1-a-reference-queue-and-weak-id-hash-for-jruby/185031 "2010-03-12T02:49:05Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![headius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/7c8e57/32.png) [@headius](https://www.ruby-forum.com/u/headius)\
**Post date:** [March 12, 2010, 2:49am UTC](https://www.ruby-forum.com/t/ann-weakling-0-0-1-a-reference-queue-and-weak-id-hash-for-jruby/185031/1 "2010-03-12T02:49:05Z")

</div>

Hi all!

JRuby 1.5 will soon be released, in which ObjectSpace.\_id2ref does not  
function unless ObjectSpace is turned on. Since the typical pattern  
for \_id2ref is to have a weak-valued hash of numeric IDs to objects,  
I’ve released a simple extension to JRuby that provides a weak  
id-to-object hash implementation plus support for weak reference  
queues. Comments, questions, suggestions are welcome.

Announcing the “weakling” library!

weakling: a collection of weakref utilities for Ruby (JRuby only for the  
moment)

== Summary ==

This library provides:

- a modified WeakRef implementation for JRuby that supports a reference  
queue
- a WeakRef::RefQueue class
- a weak-valued ID map to replace typical uses of \_id2ref

In the future it may provide additional features like a generic  
WeakHash or other reference types like soft and phantom references.

The weakling repository is at [GitHub - headius/weakling: A modified WeakRef impl for JRuby plus some weakref-related tools](http://github.com/headius/weakling).

== Usage ==

Just require ‘weakling’. It will require ‘weakref’ along with the  
refqueue JRuby extension and the weakling/collections library  
containing the weak id hash.

== RefQueue Example ==

require ‘weakling’  
require ‘java’

q = WeakRef::RefQueue.new  
wr = WeakRef.new(Object.new, q)  
puts “weakref object: #{wr. **id** }”

puts “running GC”  
java.lang.System.gc

puts “weakref alive?: #{wr.weakref\_alive?}”  
puts “weakref object from queue: #{q.poll. **id** }”

== WeakRef::IdHash Example ==

require ‘weakling’

wh = WeakRef::IdHash.new

ary = (1…10).to\_a.map {Object.new}  
ids = ary.map {|o| wh.add(o)}

puts “all items in weak\_id\_hash:”  
ids.each {|i| puts “#{i} = #{wh[i]}”}

puts “dereferencing objects”  
ary = nil

puts “forcing GC”  
begin  
require ‘java’  
java.lang.System.gc  
rescue  
GC.start  
end

puts “all items in weak id hash:”  
ids.each {|i| puts “#{i} = #{wh[i]}”}

* * *

To unsubscribe from this list, please visit:

```
http://xircles.codehaus.org/manage_email

```
