0

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 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
Morrowless
  • 6,856
  • 11
  • 51
  • 81

2 Answers2

6
sleep 0.5 until arr.all? &:running?
Simon Ernst
  • 1,430
  • 9
  • 10
0

How about

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

eugen
  • 8,916
  • 11
  • 57
  • 65
  • You should replace your code in the context, it only replaces the inner loop but you give the impression that it replaces the whole code. – Benoit Garret Sep 08 '11 at 08:20