ruby 1.8.7 readline $stdout conflict

249 views Asked by At

Fellow Ruby developers.

I am building a prompt interface with Ruby 1.8.7. As such, I'm using 'readline' in my codes.

As it is a prompt interface I'm building, I want to create an option such that, should a user specify a logfile, all STDOUT and STDERR gets tee'd to said logfile as well as gets displayed on prompt (default STDOUT and STDERR).

I created a class "TeeIo" to do this. This example applies to STDOUT, but same would apply to STDERR. See below:

class TeeIo < IO     

    def initialize orig, file
      @orig = orig
      @file = file   
    end

    def write string
      @file.write string
      @orig.write string   
    end   
end

tee = TeeIo.new $stdout, File.new('foo', 'w') 
$stdout = tee

Then, in my code, I create a TeeIo object and assign it to $stdout. I prefer this approach because I used gems in my code, and I don't get full control to all the STDOUT / STDERR activities (some are embedded in gem code, other libraries, etc.)

When I do so, I got "uninitialized stream" errors. Tracing that points the error back to readline.

I know that the irb uses readlines to read from prompt. As such, I tested the above portion of code on irb. I got a more elaborate message this time:

/arm/tools/ruby/ruby/1.8.7/rhe5-x86_64/lib/ruby/1.8/irb/input-method.rb:97:in `readline': uninitialized stream (IOError)

Does anyone know why doing what I did above affects 'readline' input stream?

Many thanks for your help!

0

There are 0 answers