On Wed, 8 Nov 2006, James Smith wrote:

> Hi,
>
> I would basically like to be able to run a ruby program from my rails
> application and be able to read from it and write to it accordingly.
>
> OK, i am currently using:
>
> IO.popen("ruby ruby_program.rb", "r+") do |f|
> @read = f.read
> @write = f.write  #this is where i need help
> end
>
> say if the external ruby program was as follows:
>
> 0  def readWrite
> 1  puts "printing.."
> 2  @string = gets
> 3  end
> 4  readWrite
>
> I can currently read (in example) "printing..", however when it reaches
> line 2 of the ruby program, i can't seem to write to it, and my rails
> program just waits. I need to use something that says "read from program
> until it is waiting for input, and then write to it" (also, when writing
> to the program, how do i emulate the user pressing enter - \n? )
>
> Thanks

this is a terrifically bad idea, but here's how you'd do it


     harp:~ > cat a.rb
     IO.popen("ruby ruby_program.rb", "r+") do |f|
       @read = f.gets
       p @read
       @write = f.puts 42
     end

     harp:~ > cat ruby_program.rb
     STDOUT.sync = true

     def readWrite
       puts "printing.."
       @string = gets
     end

     readWrite


     harp:~ > ruby a.rb
     "printing..\n"


you were calling read, which reads till the end of input and that's not going
to occur until the program exits.

i highly reccomend post exatly what you are trying to accomplish so we can try
to come up with a safer solution: spawning processes from a web-app like this
will cause problems unless you are __very__ comfortable with ipc.

cheers.

-a
-- 
my religion is very simple.  my religion is kindness. -- the dalai lama