logo       

[ruby-list:42082] Re: puts,printの出力をファイルにも出力するには: msg#00113

lang.ruby.japanese

Subject: [ruby-list:42082] Re: puts,printの出力をファイルにも出力するには

Gimiteです。

> 単純なトップレベルでのputsの再定義ですが、
> alias old_puts puts
> alias old_print print
> def set_output_file(file)
> $h_output=open(file,"w")
> end
>
> def puts(*arg)
> $h_output.old_puts *arg
> STDOUT.old_puts *arg
> end

これとあまり変わりませんが、

$out_file= open("log.txt", "w")

def $stdout.write(str)
super(str)
$out_file.write(str)
end

とすれば、$stdoutに出力する関数(putsとかprintとかprintfとか)の挙動を
いっぺんに変更できるようです。


--
Gimite 市川 <gimite-6zHM6C0iXZ9UD6l512u9tw@xxxxxxxxxxxxxxxx>




<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise