From: William James Date: 2006-07-30T03:55:42+09:00 Subject: Re: Loop Timing and Constant Input Picklegnome wrote: > A few quick questions: > > How can I make a loop that runs at, say, 8Hz? That is, every 1/8 of a > second, it executes the loop. I could put a pause at the end of the loop > (what is the syntax for making the script wait a certain amount of time > before continuing?) but this is rather imprecise - I want the loop to take > 1/8 of a second, including all processing time. span = 1.0 / 8 99.times { start_time = Time.now print "." sleep span - (Time.now - start_time) } > > Secondly, how can I get input from the keyboard without the user pressing > enter? "What should I feed my pet?" The answer would depend upon what kind of pet you have. "What sort of glue should I use to repair my broken chess piece?" That may depend upon the material of which the piece is made. For windoze: require 'Win32API' Kbhit = Win32API.new("msvcrt", "_kbhit", [], 'I') Getch = Win32API.new("msvcrt", "_getch", [], 'I') def get_key sleep 0.02 return nil if Kbhit.call.zero? c = Getch.call c = Getch.call + 256 if c.zero? || c == 0xE0 c end