rails - Redirecting console output to a file

console, file, ruby, ruby-on-rails

Solution

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

To restore:

$stdout = STDOUT

Problem

On a bash console, if I do this: ``` cd mydir ls -l > 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. Is there any way to do something similar on the rails console? 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.

Original source

Related problems