From: Geoff Stanley Date: 2006-05-24T02:22:09+09:00 Subject: Re: IRB'ing within a program Logan Capaldo wrote: > Here's a "solution" involving IO.popen: > > % cat irb_remote.rb > puts "Welcome to my pseudo irb frontend. Good luck with this." > > IO.popen('irb', 'r+') do |irb| > while line = gets > irb.puts line > puts irb.gets > end > end Hal Fulton wrote: > Well, how about starting irb in another process and talking to it > via pipes? Logan and Hal, this sounds like a good idea. However, as Logan's code illustrated, pipes are difficult to get working right. I was just playing around with them myself; I've discovered a few problems that might not be overcome. First, the code that I was using: irb = IO.popen('irb', 'r+') loop do print ">> " irb.puts gets irb.gets # Gets the line that we just put. puts "=> #{irb.gets.chomp.inspect}" end Now, the problems: 1) Everything we get back is a string. 1+2 should return the Fixnum 3, but we read that Fixnum 3 through the pipe as a string. To overcome this, we'd have to know what type of value we should get in return. That's basically redoing a good portion of irb. 2) This code doesn't get lines in the right proportion. It works for basic things like '1+2' and 'foo = 42', but for a multiline piece of code, such as a method definition, it breaks down: >> def foo puts "bar" end foo ....... hello? As you can see, I got no output back. Any way popen could be used more effectively? I don't want to have to reimplement irb -- that would be a lot of work, I imagine. -- Posted via http://www.ruby-forum.com/.