From: Nikodemus Siivola Date: 2002-05-10T19:22:30+09:00 Subject: Re: timeout.rb problem On Fri, 10 May 2002 nobu.nokada@softhome.net wrote: > > I am wondering if there should be a companion method, such as > > "timebreak" that merely rudely interrupts the execution of a the block > > instead of raising an exception. > Like a signal handler? Like: #!/usr/bin/ruby def timebreak(sec) res = nil sub = Thread.start { res = yield sec } sleep sec # This is a bit simplistic: we should sub.kill if sub.alive? # return before "sec" seconds if the block return res # has finished execution, not just hang for end # the given time... if __FILE__ == $0 p timebreak(1) { sleep 2 "killed asleep" } p timebreak(2) { sleep 1 "done in time" } end __END__