class Sequence::WeakHash

i thought ruby had this already, but i can't find it…

Public Class Methods

new(hash={}) click to toggle source

dunno if this is thread-safe

# File lib/sequence/functional.rb, line 8
def initialize hash={},default=nil,&block
  @hash=block ? Hash.new(&block) : Hash.new(default)
  hash.each{|(k,v)|
    self[k]=v    
  }
end

Public Instance Methods

[](key) click to toggle source
# File lib/sequence/functional.rb, line 20
def [] key
  @hash[key.__id__]
end
[]=(key, val) click to toggle source
# File lib/sequence/functional.rb, line 24
def []= key, val
  delete_when_dies key
  @hash[key.__id__]=val
end
delete(key) click to toggle source
# File lib/sequence/functional.rb, line 29
def delete key
  @hash.delete key.__id__
end
delete_when_dies(key) click to toggle source
# File lib/sequence/functional.rb, line 15
def delete_when_dies key
  ObjectSpace.define_finalizer(key){|id| @hash.include? id and @hash.delete id}  
  return key
end
keys() click to toggle source
# File lib/sequence/functional.rb, line 37
def keys
  @hash.keys.map!{|id| ObjectSpace._id2ref(id)}
end
values() click to toggle source
# File lib/sequence/functional.rb, line 33
def values
  @hash.values
end