缩短枚举代码

时间:2011-09-08 08:07:40

标签: ruby enumeration ruby-1.9.2

在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

2 个答案:

答案 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? }