0

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!

  • You do know that Ruby is currently at 2.1+ and 1.8.7 was deprecated a long time ago. In other words, it's no longer supported or patched. – the Tin Man Nov 12 '14 at 19:24
  • Yes, I know. I'm afraid that there is a reason I must remain at 1.8.7. I'm afraid my development environment is too immense for me to migrate to 2.1+. The current development has over 100k lines of codes! – ballade_op23 Nov 13 '14 at 16:39
  • If your code is pure Ruby, then it should be easy to get it running on more recent Rubies. They've done a great job maintaining compatibility as they moved from 1.8.7 to 2+. I have never run into a problem. – the Tin Man Nov 13 '14 at 16:48
  • I really wished that were the case. For example, we have Rdoc usage scattered all over the code hierarchy (Rdoc feature was removed in later version). Our setup was done with autoload (that also changed). Many more. Right now we are just trying to find the path of least resistance. It doesn't look like transition to 2.1+ will be smooth sailing. – ballade_op23 Nov 13 '14 at 21:42
  • Hi. Have you found any solution with this error? – Dmitriy Mitiai Jan 28 '21 at 07:55

0 Answers0