我排到STDOUT使用該代碼的文件:對外輸出標準輸出到一個文件,然後再返回
STDOUT.reopen(File.open("./OUTPUT",'w+'))
現在我需要的路線STDOUT到終端一次。
我該怎麼做?
我排到STDOUT使用該代碼的文件:對外輸出標準輸出到一個文件,然後再返回
STDOUT.reopen(File.open("./OUTPUT",'w+'))
現在我需要的路線STDOUT到終端一次。
我該怎麼做?
修訂
orig_std_out = STDOUT.clone
STDOUT.reopen(File.open('OUTPUT', 'w+'))
puts "test to file"
STDOUT.reopen(orig_std_out)
puts "test to screen"
您需要重新打開STDOUT的文件句柄1,這是標準輸出(0=stdin, 1=stdout, 2=stderr
)標準FD手柄。
我不是一個Ruby的傢伙,但我認爲這是對的:
STDOUT.reopen(IO.for_fd(1, "r"))
更簡單,如果在UNIX上:
STDOUT.reopen 'OUTPUT'
puts 'text to file'
STDOUT.reopen '/dev/tty'
puts 'text to console'