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? }
精彩评论