David Masover
4/2/2009 11:14:00 PM
On Thursday 02 April 2009 16:46:37 Eleanor McHugh wrote:
> 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*
It could also be written like this, if I understand it:
hs.delete_if { |k, v| (v.map! { |worker| thread_dead?(worker) ? (cleaned += 1;
nil) : worker }.tap(&:compact!).empty? }
I don't feel strongly about compact! -- after all, it may be useful to know
whether it found anything. But this is exactly what tap is for, right?
For those on 1.8.6:
class Object
def tap
yield self
self
end
end