I'm trying to read one character at a time in a ruby running in cygwin.
STDIN.getc
returns the characters but only after I pressed enter:
STDOUT.sync = t开发者_开发技巧rue
while true
STDIN.getc
puts "HELLO"
STDOUT.flush
end
test session:
aa
HELLO
HELLO
HELLO
How can I read a character?
use io-console, at later Ruby1.9.3
require 'io/console'
# input 3 chars and escape
buf = ''
3.times do
buf << STDIN.getch
end
print "Your input is '#{buf}'"
精彩评论