J
Jean-Julien Fleck
Hello,
I'm stuck with a problem concerning IO.popen
It's not necessarily related to irb but that's a good point where to start.
I'd like to drive irb from ruby using IO.popen. The problem is: each
time I type something inside irb, I can get one, two or more lines of
output I'd like to store with no way to know beforehand how many there
will be.
The thing is: I don't know how to detect when irb has ended its output
and is waiting for me to give some input.
I tried the following:
def irb(string)
output =3D ''
puts "Trying to talk to irb"
IO.popen('irb --simple-prompt','r+') do |io|
string.chomp.split.each do |s|
io.puts s
while line =3D io.gets
line =3D io.gets
puts line
output +=3D line
end
end
end
return output
end
s =3D "2+2\n'2'+'2'\n'2'+2"
irb(s)
I was hoping that io.gets would be nil when irb is waiting for input,
but it's not. So this little program gets stuck after the first
statement:
brisingr ~/tmp>ruby essai_irb.rb
Trying to talk to irb
=3D> 4
Rather, I would like him to return
TypeError: can't convert Fixnum into String
from (irb):4:in `+'
from (irb):4
Does someone have a workaround ?
Thanks,
--=20
JJ Fleck
PCSI1 Lyc=E9e Kl=E9ber
I'm stuck with a problem concerning IO.popen
It's not necessarily related to irb but that's a good point where to start.
I'd like to drive irb from ruby using IO.popen. The problem is: each
time I type something inside irb, I can get one, two or more lines of
output I'd like to store with no way to know beforehand how many there
will be.
The thing is: I don't know how to detect when irb has ended its output
and is waiting for me to give some input.
I tried the following:
def irb(string)
output =3D ''
puts "Trying to talk to irb"
IO.popen('irb --simple-prompt','r+') do |io|
string.chomp.split.each do |s|
io.puts s
while line =3D io.gets
line =3D io.gets
puts line
output +=3D line
end
end
end
return output
end
s =3D "2+2\n'2'+'2'\n'2'+2"
irb(s)
I was hoping that io.gets would be nil when irb is waiting for input,
but it's not. So this little program gets stuck after the first
statement:
brisingr ~/tmp>ruby essai_irb.rb
Trying to talk to irb
=3D> 4
Rather, I would like him to return
TypeError: can't convert Fixnum into String
from (irb):4:in `+'
from (irb):4
Does someone have a workaround ?
Thanks,
--=20
JJ Fleck
PCSI1 Lyc=E9e Kl=E9ber