lib/wref/map.rb in wref-0.0.8 vs lib/wref/map.rb in wref-0.0.9

- old
+ new

@@ -68,11 +68,12 @@ rescue Wref::Recycled return nil end end - #Scans the whole map and removes dead references. After the implementation of automatic clean-up by using ObjectSpace.define_finalizer, there should be no reason to call this method. + # Scans the whole map and removes dead references. After the implementation of automatic clean-up by using ObjectSpace.define_finalizer, + # there should be no reason to call this method. def clean keys = nil @mutex.synchronize do keys = @map.keys end @@ -155,9 +156,51 @@ end ids.each do |id| if obj = get(id) yielder << [id, obj] + end + end + end + + if block + enum.each(&block) + else + return enum + end + end + + def each_key(&block) + enum = Enumerator.new do |yielder| + ids = nil + @mutex.synchronize do + ids = @map.keys + end + + ids.each do |id| + if obj = get(id) + yielder << id + end + end + end + + if block + enum.each(&block) + else + return enum + end + end + + def each_value(&block) + enum = Enumerator.new do |yielder| + ids = nil + @mutex.synchronize do + ids = @map.keys + end + + ids.each do |id| + if obj = get(id) + yielder << obj end end end if block