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.

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

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.

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

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