2009-09-24 9 views

risposta

15

AGGIORNATO

orig_std_out = STDOUT.clone 
STDOUT.reopen(File.open('OUTPUT', 'w+')) 
puts "test to file" 
STDOUT.reopen(orig_std_out) 
puts "test to screen" 
2

È necessario riaprire STDOUT sulla maniglia di file 1, che è il manico fd standard per stdout (0=stdin, 1=stdout, 2=stderr).

Io non sono un ragazzo di Ruby, ma credo che questo è circa la destra:

STDOUT.reopen(IO.for_fd(1, "r")) 
1

Ancora più semplice se su UNIX:

STDOUT.reopen 'OUTPUT' 
puts 'text to file' 

STDOUT.reopen '/dev/tty' 
puts 'text to console'