tags:

views:

51

answers:

1

How to write and read from process with Ruby? I wrote this, but it didn`t work...

output = IO.popen("irb", "r+") do |pipe|
  pipe.gets
  pipe.puts "10**6"
  pipe.gets
  pipe.puts "quit"
end

I rewrite so

IO.popen("irb", "w+") do |pipe|
  3.times {puts pipe.gets} # startup noise
  pipe.puts "10**6\n"
  puts pipe.gets # I expect " => 1000000"
  pipe.puts "quit" # I expect exit from irb
end 
but It didn`t work too

+2  A: 

Either do

IO.popen("ruby", "r+") do |pipe|
  pipe.puts "puts 10**6"
  pipe.puts "__END__"
  pipe.gets
end

or do

IO.popen("irb", "r+") do |pipe|
  pipe.puts "\n"
  3.times {pipe.gets} # startup noise
  pipe.puts "puts 10**6\n"
  pipe.gets # prompt
  pipe.gets
end
Tass
I rewrite so <pre>IO.popen("irb", "r+") do |pipe| 3.times {puts pipe.gets} # startup noise pipe.puts "10**6\n" puts pipe.gets # I expect " => 1000000" pipe.puts "quit" # I expect exit from irbend </pre> but It didn`t work too
mystdeim
Try with `2.times` instead of `3.times`. I got a `puts` in my `.irbrc`.
Tass
nothing :( and I can`t exit from irb...
mystdeim
Only output: Switch to inspect mode.
mystdeim
Let's just leave it at 'irb is not to be played with'? What are you trying to do exactly?
Tass
I found open3, it`s OK for me
mystdeim