From: patrick-may@... (Patrick May) Date: 2002-08-18T17:23:18+09:00 Subject: Re: How do I dup file descriptors in ruby? (diverting STDERR) Richard Ryan wrote in message news:... > Thanks for the suggestion. The problem with metaphor is that the > intended idea can be too easily miscommunicated. By ``symphony'' > I was referring to the complexity of the code structure. If by complexity you mean not calling the same function 6 times with different args, then by all means continue using your prefered tool: exec 3>&1 exec 4>&2 exec 1> /tmp/logfile.out 2>&1 # ... do stuff exec 1>&3 exec 2>&4 exec 3>&- exec 4>&- > Anyway, that being said, if you put the merging of stdout together > with stderr into the logfile which is in my second example, you'll > probably get back at least the same number of lines, and you'll still > have the complexity (if not quite a bit more). True, the simplist ruby example: err = $stderr out = $stdout open( "logfile.text", "a" ) do |file| $stdout = $stderr = file # ... do stuff end $stderr = err $stdout = out However, if this is a common operation for you, Ruby allows you to improve on the above by abstracting the functionality and letting you pull it in. That's what the require 'redirect' solution is about. > Also, please keep in > mind that I am thinking of putting a significant amount of code inside > of the redirection and merging---I would definitely like to avoid the > extra block if at all possible. Which block is extra, and what is wrong with it? > Also, please note that in my second > example I ``un-merge'' stdout from stderr when I'm done. so have all the examples ~ Patrick