开发者

Shortening enumeration code

开发者 https://www.devze.com 2023-04-03 16:45 出处:网络
What is a succinct way of accomplishing this in Ruby? I simply want to wait until all objects are running. This seems too wordy.

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

精彩评论

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