BrainLikeADullPencil
BrainLikeADullPencil

Reputation: 11673

Ruby: capturing output

In my attempt to learn more about Ruby, I'm looking at this blog post http://mentalized.net/journal/2010/03/08/5_ways_to_run_commands_from_ruby/ which talks about different commands-executing methods such as Kernel#exec and Kernel#system. His example file has two lines of code

  #!/usr/bin/env ruby
puts "out"
STDERR.puts "error"

and he notes, whenever he runs a command, whether the output is captured or not. For example, here STDOUT is captured, but STDERR is not.

 >> `./err.rb`
err
=> "out\n"

But why is this important? What is the difference in practical terms whether output is "captured" or not. I tried to find the answer to this in my Ruby book, but to no avail.

Thanks for your explanation.

Upvotes: 3

Views: 253

Answers (1)

michaelmichael
michaelmichael

Reputation: 14135

The author notes the significance of whether output is captured or not at the top of the post you link:

[...] it was triggered by an issue with my Redmine Github Hook plugin where STDERR messages were not being logged.

Sounds like the author found that error messages written to stderr were not being "captured" and put into the program's log file, which may have made diagnosing those errors more difficult.

If you haven't yet, you might want to learn a little bit more about standard streams, which determine where your program's output is directed. If you're able to manipulate standard streams effectively you can capture output from your program and redirect it to the appropriate place, e.g. errors go to a log file, generated data outputs to a data file, status updates that a user might want to see go to the screen, etc.

Upvotes: 2

Related Questions