multithreading - Printing in a Ruby Thread while waiting for input generates weird output -
so working in ruby , idea reprint set of strings on until key pressed. code that
frame = "aaaa bbbb cccc dddd" thread = thread.new while(true) print frame sleep(0.5) end end thread.run begin system("stty raw -echo") str = stdin.getc ensure system("stty -raw echo") end thread.kill
when code executes, generates output
aaaa bbbb cccc ddddaaaa bbbb cccc ddddaaaa bbbb cccc ddddaaaa bbbb cccc ddddaaaa bbbb cccc dddd
obviously, think should produce
aaaa bbbb cccc dddd
repeating until key pressed, , can't figure out why doesn't. thoughts?
the following code works expected me, including printing newlines. if works correctly you, have misidentified cause of problems.
swivel = thread.new loop print "hello\nworld" sleep 0.5 end end.run puts "press enter stop" str = stdin.gets swivel.kill
edit: when call stty raw -echo
, \n
characters go down line. need \r\n
first go front of line, , \n
go down next line. presumably source file saved "unix line endings" (\n
only), , newlines literally embedded in string not sufficient.
Comments
Post a Comment