In the one answer I have found this stanza that waits for your input and prints it until you press enter:
require 'io/console'
require 'io/wait'
loop do
chars = STDIN.getch
chars << STDIN.getch while STDIN.ready? # Process multi-char paste
break if chars == ?\n
STDOUT.print chars
end
However, in order to exit loop
, I must press "Enter"(key for new line - \n
) twice, or press something else after it.
When I try to execute the same loop again (copy-paste it into the same pry session) I am getting:
IOError: byte oriented read for character buffered IO
chars << STDIN.getch while STDIN.ready?
cause raising, mentioned above, error. Without this line, ruby just doesn't show any error.
In both cases (with and without above line), in the loop:
when I press the enter and then some letter (for example 'z') I'm getting this error.
In the next loop, above letter will show (without my input).when I press the enter twice - no error, it will exit.
In the next loop when I press some letter, error will show.
In the next loop above letter will show
I remember that in C or C++ there was flush
so you can empty the buffer. I have found s few methods, and tried like this:
loop do
STDIN.ioflush
STDOUT.ioflush
STDOUT.iflush
STDIN.iflush
STDOUT.oflush
STDIN.oflush
chars = STDIN.getch
chars << STDIN.getch while STDIN.ready?
break if chars == ?\n
STDOUT.print chars
end
but it didn't work.
How to solve this behavior with enter and 2nd letter & IOError
. I think both are correlated in some way.