Wref

Weak references for Ruby

Install

Add to your Gemfile and bundle

gem "wref"

Usage

Make a new weak reference

str = "Test"
weak_ref = Wref.new(str)

Check if reference is still alive

weak_ref.alive? #=> true | false

Spawn a weak map

weak_map = Wref::Map.new

Set a key and value in a weak map

str = "Test"
map[1] = str
map.set(1, str)

Get values from a weak map

map.get(1) #=> "Test" | nil
map.get!(1) #=> "Test" | Wref::Recycled error

Loop over all valid pairs in a weak map

map.each do |key, value|
  puts "Valid pair: #{key}: #{value}"
end

Check if key is valid in a weak map.

map.valid?(1) #=> true | false

Getting length of both valid and invalid and the current time in a weak map (fastest)

map.length #=> 1

Getting length of valid options in a weak map

map.length_valid #=> 0

Get from a key

map.get(1) #=> "Test" | nil
map.get!(1) #=> "Test" | Error - Wref::Recycled

Delete a key from a weak map

map.delete(1) #=> "Test" | nil if recycled

Delete all recycled options

map.clean

Contributing to wref

Copyright © 2012 Kasper Johansen. See LICENSE.txt for further details.