brian said:
Ok thats one way to do it, but doesn't ruby have an actual way of just
seeing if any input has been performed? Unnecessary threads will just
slow down the program.
A thread won't cost much if it is just waiting for input.
If the other tasks that are being handled by your program are driven by
IO, then you could use select from a single one thread. (That's more or
less what is going on when you have multiple ruby threads each handling
their own IO.)
Or you could use select with a timeout of 0 to check if input is
available on $stdin.
print "type something> "; $stdout.flush
loop do
sleep 0.1; print "."; $stdout.flush
if IO.select([$stdin], [], [], 0)
str = $stdin.gets
break unless str
puts "You typed #{str.inspect}"
print "type something> "; $stdout.flush
end
end
The #sleep call and the print "." is just to emulate the effect of the
program running while the user is typing.
However, I expect that it will be easier to make the program feel
responsive if you use threads, and let ruby's thread scheduler handle
the timing.