How to write console output into Text file in Ruby?

Hi,

Could someone suggest me to how to write console output into Text file
Ruby?
I want to deal with the console output of the Ruby script. Please tell
me
how to do that?

Please help. Thanks in advance.

Thanks and Regards,
Amit

f = File.open(‘text.txt’, ‘w’)

old_out = $stdout
$stdout = f

puts ‘hello’
puts ‘world’

f.close

$stdout = old_out
puts ‘goodbye’

–output:–

$ ruby ruby.rb
goodbye
$ cat text.txt
hello
world

No one can answer that question. Your question is akin to asking: was
multiplication or division used to get the number 6.

Ok. Thanks for your reply. Still I will be trying to get the solution.
:slight_smile:

Thanks,
-Amit

On Sat, Aug 6, 2011 at 1:49 PM, 7stud – [email protected] wrote:

To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.


Thanks and Regards,
Amit

Thanks for your prompt reply. This is really helpful. But if I want to
write
the following output to the text file then how should I achieve this?

Output:

Loaded suite simple
Started
.
Finished in 0.203125 seconds.
1 tests, 1 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0
notifications
100% passed
4.92 tests/s, 4.92 assertions/s

Thanks,
Amit.

On Sat, Aug 6, 2011 at 1:16 PM, 7stud – [email protected] wrote:

$stdout = old_out


Thanks and Regards,
Amit

The point is: what did you do to get that output??? OKAY??!