From: Eleanor McHugh Date: 2009-04-03T06:46:37+09:00 Subject: Re: Nilling for GC On 2 Apr 2009, at 21:14, Paganoni wrote: > le 02/04/2009 10:27, Eleanor McHugh nous a dit: > >> You could write something like: >> def thread_dead? worker >> (Time.now - worker[:started_at] > 5) && (worker[:thread].status >> == false) >> end >> @semaphore.synchronize do >> hs.delete_if do |k, v| >> v.map! { |worker| thread_dead?(worker) ? (cleaned += 1; nil) : >> worker }.compact! >> v.empty? >> end >> end > > Your delete_if solution is quite elegant because self explanatory ! Thank you :) The delete_if could also be written as a one-liner, but it looks uglier: hs.delete_if { |k, v| (v.map! { |worker| thread_dead?(worker) ? (cleaned += 1; nil) : worker }.compact! || v).empty? } It always bugs me that compact! returns nil if no changes occur, rather than the enum as I seem to use this particular idiom a lot *sigh* Ellie Eleanor McHugh Games With Brains http://slides.games-with-brains.net ---- raise ArgumentError unless @reality.responds_to? :reason