From: Brian Candler Date: 2010-06-17T05:15:03+09:00 Subject: Re: Simulating keystrokes for automated user input Ryan Mohr wrote: > Brian Candler wrote: >> Spawn a pty. There's pty.so in the standard library, which is a C >> extension and unfortunately not well documented, but google for >> "PTY.spawn" to find some examples, e.g. > > Thanks for the suggestion Brian but I don't think that will work for my > case. I need to send keystrokes to stdin of the current process itself. Then I think you're stuck. When your ruby process was started, its stdin file descriptor was connected to something (e.g. a terminal or a pipe). Only that thing can generate data for your app to read; e.g. you can't write to the read end of a pipe. You could connect forcibly reopen stdin on a different fd: rd, wr = IO.pipe STDIN.reopen(rd) Thread.new do wr.puts "hello" end line = gets puts line.inspect That's really horrible though. It would be better to fork your program entirely: IO.popen("-","rb+") do |pipe| if pipe # parent pipe.puts "hello" res = pipe.gets puts "got: #{res}" else # child line = gets puts line.inspect end end If what you're trying to accomplish is unit testing of a CLI-driven program, then I'd suggest either running the program under test as a child (e.g. using IO.popen or PTY.spawn), or factor out your objects so that they can read and write to arbitrary IO objects that you pass in, so that you can pass them a StringIO or one end of a pipe. Otherwise, perhaps you can explain what you are trying to achieve? Regards, Brian. -- Posted via http://www.ruby-forum.com/.