在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
答案 0 :(得分:6)
sleep 0.5 until arr.all? &:running?
答案 1 :(得分:0)
怎么样
sleep(0.5) until arr.inject(true) { |all_running, obj| all_running and obj.running? }