From: nobu.nokada@... Date: 2004-05-31T18:19:12+09:00 Subject: Re: elegant way to say "try this thing, one at a time, until condition is met" Hi, At Mon, 31 May 2004 17:31:24 +0900, David Garamond wrote in [ruby-talk:101855]: > > Example: I need to unmount /usr and /usr2, but currently I can't because > there are some opened files from those partitions. I am going to try > shutting down daemons or killing some processes (or other things), one > at a time, until I can unmount it. > > def kill(process, signal) > ... > end > > # returns false if failed, true if succeeded or already unmounted > def unmount(partition) > ... > end > > unless unmount("/usr") && unmount("/usr2") > system "/etc/init.d/httpd stop" > system "/etc/init.d/squid stop" > unless unmount("/usr") && unmount("/usr2") > kill("rdiff-backup", :TERM) > sleep 3 > unless unmount("/usr") && unmount("/usr2") > kill("rdiff-backup", :KILL) > sleep 3 > ... > ... > end > end > end What about: count = 0 until unmount("/usr") && unmount("/usr2") case count += 1 when 1 system "/etc/init.d/httpd stop" system "/etc/init.d/squid stop" when 2 kill("rdiff-backup", :TERM) sleep 3 when 3 kill("rdiff-backup", :KILL) sleep 3 end end or: stopper = [ proc { system "/etc/init.d/httpd stop" system "/etc/init.d/squid stop" }, proc { kill("rdiff-backup", :TERM) sleep 3 }, proc { kill("rdiff-backup", :KILL) sleep 3 } ] until unmount("/usr") && unmount("/usr2") stopper.shift.call end -- Nobu Nakada