开发者

ruby read character in cygwin terminal

开发者 https://www.devze.com 2023-04-02 13:19 出处:网络
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:

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}'"
0

精彩评论

暂无评论...
验证码 换一张
取 消