缩短枚举代码



在Ruby中实现这一点的简洁方法是什么?我只是想等待,直到所有对象都在运行。这似乎太啰嗦了。

# arr contains objects that respond to running?
all_running = false
until all_running
    sleep 0.5
    all_running = true
    arr.each{ |obj|
        all_running = all_running and obj.running?
    }
end
sleep 0.5 until arr.all? &:running?

sleep(0.5) until arr.inject(true) { |all_running, obj| all_running and obj.running? }

相关内容

  • 没有找到相关文章

最新更新