From: ara.t.howard@... Date: 2006-05-24T02:43:08+09:00 Subject: Re: IRB'ing within a program On Wed, 24 May 2006, Geoff Stanley wrote: > 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. you are trying way to hard: harp:~ > cat a.rb system 'irb' puts 'done' harp:~ > ruby a.rb irb(main):001:0> def foo irb(main):002:1> 42 irb(main):003:1> end => nil irb(main):004:0> foo => 42 irb(main):005:0> exit done irb uses readline so you need to use pty to control it. otherwise you'll not see the prompt. -a -- be kind whenever possible... it is always possible. - h.h. the 14th dali lama