Class: Weakling::IdHash
- Inherits:
-
Object
- Object
- Weakling::IdHash
- Includes:
- Enumerable
- Defined in:
- lib/weakling/collections.rb
Defined Under Namespace
Classes: IdWeakRef
Instance Method Summary collapse
- #[](id) ⇒ Object
- #_cleanup ⇒ Object
- #add(object) ⇒ Object
- #each ⇒ Object
-
#initialize ⇒ IdHash
constructor
A new instance of IdHash.
Constructor Details
Instance Method Details
#[](id) ⇒ Object
20 21 22 23 24 25 26 27 |
# File 'lib/weakling/collections.rb', line 20 def [](id) _cleanup if wr = @hash[id] return wr.get rescue nil end return nil end |
#_cleanup ⇒ Object
38 39 40 41 42 |
# File 'lib/weakling/collections.rb', line 38 def _cleanup while ref = @queue.poll @hash.delete(ref.id) end end |
#add(object) ⇒ Object
29 30 31 32 33 34 35 36 |
# File 'lib/weakling/collections.rb', line 29 def add(object) _cleanup wr = IdWeakRef.new(object, @queue) @hash[wr.id] = wr return wr.id end |
#each ⇒ Object
44 45 46 |
# File 'lib/weakling/collections.rb', line 44 def each @hash.each {|id, wr| obj = wr.get rescue nil; yield [id,obj] if obj} end |