Shin guey Wong wrote:

> Anyone has any idea how to do this?
> I have try to redirect the output like STDOUT.repopen("c:\tmp.log",
> "w"), but i will not get the output on the console.

No answer? well, I just found an ugly hack on myself....
heres the code, just put it in the .irbrc file and you will get those 2 
functions, log, nolog for free...:P

For the STDIN, I am really no idea how to get it, I had to hack the IRB 
to get it work, here, I only get the STDIN from the Readline(if the user 
didn't enable readline in IRB, they will never get the stdin log in the 
file.

Did anyone has better idea on accomplish this? How to get the STDIN from 
the IRB  history?

module Kernel
def log(file_name, attr)
  $irb_log_file = File.open(file_name, attr)
end

def nolog
  if ($irb_log_file )
    $irb_log_file.close
  end
  $irb_log_file = nil
end
end

class << STDOUT
    alias :old_write :write
    def write(*args)
      if ($irb_log_file)
        $irb_log_file.write args
      end
      old_write(args)
    end
  end

class << STDERR
    alias :old_write :write
    def write(*args)
      if ($irb_log_file)
        $irb_log_file.write args
      end
      old_write(args)
    end
  end

module IRB
  class ReadlineInputMethod < InputMethod
    alias :old_gets :gets
    def gets
      l = old_gets
      if($irb_log_file)
        $irb_log_file.puts @prompt << l
      end
      l
    end
  end
end

Here is how you use it:
---------------------------
>> log 'c:\irb.log', 'w'
=> #<File:c:\irb.log>
>> puts 'hehe'
hehe
=> nil
>> puts 'hello'
hello
=> nil
>> nolog
=> nil
>>

And the log file:
--------------------
=> #<File:c:\irb.log>
>> puts 'hehe'
hehe
=> nil
>> puts 'hello'
hello
=> nil
>> nolog

-- 
Posted via http://www.ruby-forum.com/.