rails - 将控制台输出重定向到文件 [英] rails - Redirecting console output to a file

查看:20
本文介绍了rails - 将控制台输出重定向到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 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.

推荐答案

你可以使用 override $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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆