开发者

Shortening enumeration code

What is a succinct way of accomplishing this in Ruby? I simply want to wait until all objects are running. This seems too wordy.

# arr contains objects th开发者_开发知识库at 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?


How about

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜