From: Ruby Quiz Date: 2006-07-14T21:48:32+09:00 Subject: [QUIZ] Negative Sleep (#87) The three rules of Ruby Quiz: 1. Please do not post any solutions or spoiler discussion for this quiz until 48 hours have passed from the time on this message. 2. Support Ruby Quiz by submitting ideas as often as you can: http://www.rubyquiz.com/ 3. Enjoy! Suggestion: A [QUIZ] in the subject of emails about the problem helps everyone on Ruby Talk follow the discussion. Please reply to the original quiz message, if you can. -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= by MenTaLguY (Inspired by recent insomnia and a #ruby-lang snippet on Anarchaia...) Let's suppose there existed a sleep which accepted negative arguments. Would it actually imply time travel? In the case of a negative argument, the obvious description of sleep's behavior (halt the thread for the given minimum number of seconds) would be trivially satisfied by behaving as if sleep(0) had been called. That's not a very interesting result. Of course, that's not the only way to define sleep. You could also view a sleep as specifying the relationship between the computations on either side of it. In that case, sleep(1) might request that the second computation begin at least a second after the completion of the first one. Negative sleeps would simply reverse the order of the two computations, sleep(-1) meaning that the first computation should begin at least a second after the completion of the second. That sounds slightly impossible to implement, doesn't it? But consider something like this: ( Computation.new { ... } + Sleep.new(-1) + Computation.new { ... } ).run Another possible definition for sleep would be a minimum delta time between the end of the first computation and the beginning of the second one. In that case, the implementation of sleep would have to ensure that the second computation above would begin executing no less than one second before the completion of the first one. Multiple threads (and the ability to predict or control a computation's duration) might prove useful. Your assignment for this quiz is twofold: 1) Devise an "interesting" definition for sleep which allows negative durations. Alternately, use one of the definitions given here. 2) Write some Ruby code demonstrating behavior which satisfies that definition. As with the above example, you needn't provide a drop-in replacement for Kernel.sleep. If your solution does involve time travel, please ensure that it isn't posted before the end of the 48-hour spoiler period (or before the beginning of the quiz).