rails - 将控制台输出重定向到文件 [英] rails - Redirecting console output to a file
问题描述
在bash控制台上,如果我这样做:
On a bash console, if I do this:
cd mydir
ls -l > mydir.txt
<>>操作符捕获标准输入并将其重定向到文件;所以我得到 mydir.txt
中的文件列表,而不是在标准输出中。
The > operator captures the standard input and redirects it to a file; so I get the listing of files in mydir.txt
instead of in the standard output.
在rails控制台上做类似的事情?
Is there any way to do something similar on the rails console?
我有一个ruby语句生成大量的打印(〜8k行),我想能够看到它完全,但控制台只记住最后1024行左右。所以我想到重定向到一个文件 - 如果任何人都知道一个更好的选择,我很耳朵。
I've got a ruby statement that generates lots of prints (~8k lines) and I'd like to be able to see it completely, but the console only "remembers" the last 1024 lines or so. So I thought about redirecting to a file - If anyone knows a better option, I'm all ears.
推荐答案
覆盖 $ stdout
以重定向控制台输出:
You can use override $stdout
to redirect the console output:
$stdout = File.new('console.out', 'w')
您可能还需要调用一次:
You may also need to call this once:
$stdout.sync = true
这将把所有输出重定向到文件。如果您要临时重定向输出,请确保您存储 $ stdout
的原始值,以便可以将其更改。
This will redirect all output to the file. If you want to temporarily redirect the output make sure that you store the original value of $stdout
so you can change it back.
这篇关于rails - 将控制台输出重定向到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!