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

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -