Getting the input of an user with gets() before he hits Enter

Hi guys. Do you know if there’s any way to get what the user is typing
with gets(), I mean I want to know what is he typing before he hits
Enter, so for example I can do some action if the input is “”, and just
wait for he to finish the sentence if not. Can it be done with a kind of
thread or something? Do you think that consulting the actual value of
$stdin with a thread running in parallel of the other thread runnig
gets() can be done?

Yes, using the ‘io/console’ library (included in stdlib). it provides,
among other things, IO#getch.

require ‘io/console’
while char = $stdin.getch
puts “You pressed ‘#{char}’!”
end

Posted by Bartosz Dziewoński (matmarex) on 2013-04-05 18:18

Yes, using the ‘io/console’ library (included in stdlib). it provides,
among other things, IO#getch.

That’s great Bartosz! thanks! I’ll try it soon.