Class: RKWeakHash

Inherits:
Object
  • Object
show all
Defined in:
lib/rk_weak_hash.rb

Overview

This is a weak hash similar in semantics to SimpleWeakHash but implemented using the standard libraries ‘delegate’ and ‘weakref’. In theory using DelegateClass and WeakRef should make this work slower than the alternative implementation (SimpleWeakHash).

Instance Method Summary collapse

Constructor Details

#initialize(cache = Hash.new) ⇒ RKWeakHash

note: I added this method so I could provide a default value (a new Hash) instead of demanding it from the user.



19
20
21
# File 'lib/rk_weak_hash.rb', line 19

def initialize(cache = Hash.new)
  super(cache)
end

Instance Method Details

#[](key) ⇒ Object

!> method redefined; discarding old []



27
28
29
# File 'lib/rk_weak_hash.rb', line 27

def [](key) # !> method redefined; discarding old []
  __getobj__[WeakRef.new(key)]
end

#[]=(key, val) ⇒ Object

!> method redefined; discarding old []=



23
24
25
# File 'lib/rk_weak_hash.rb', line 23

def []=(key,val) # !> method redefined; discarding old []=
  __getobj__[WeakRef.new(key)] = WeakRef.new(val)
end

#cleanupObject



38
39
40
# File 'lib/rk_weak_hash.rb', line 38

def cleanup
  delete_if {|k,v| k.__getobj__.nil?}
end

#each(&b) ⇒ Object

!> method redefined; discarding old each



31
32
33
34
35
36
# File 'lib/rk_weak_hash.rb', line 31

def each(&b) # !> method redefined; discarding old each
  __getobj__.each do |k,v|
    b[k.__getobj__, v.__getobj__] unless k.__getobj__.nil?
  end
  self
end