Jim Weirich <jweirich / one.net> writes: > Richard> Thank you for the suggestion, but perhaps I should have > Richard> been more specific about what I really want to do. The > Richard> following is closer to what I'm interested in. It's a > Richard> very common lick to play in Korn, perl and C and if I > Richard> have to go through a symphony to do the same thing in > Richard> ruby then, to paraphrase the great American poet Chuck > Richard> Berry, I'll lose the beauty of the melody. > > Cute analogy :-) > > [... lick elided ...] > > Hmmm ... I count 6 extra lines for your lick. > > Fortunately, in Ruby you can capture licks in a JamMan effects box and > replay the lick whenever you like. > > Write the following file one time and store it somewhere in your > standard Ruby path. > > -- File: redirect.rb ------------------------------------------------- > def redirect_to(fn) > olddefout = $defout > oldstderr = $stderr > open(fn, "a") do |file| > $defout = file > $stderr = file > begin > yield > ensure > $defout = olddefout > $stderr = oldstderr > end > end > end > ---------------------------------------------------------------------- > > Now, to playback your lick, just do ... > > require 'redirect' > redirect_to("logfile.txt") { > puts "output now goes to the log file" > } > > Just three lines of overhead instead of the six. And it handles > exceptions and early exits properly. > > -- > -- Jim Weirich jweirich / one.net http://w3.one.net/~jweirich > --------------------------------------------------------------------- > "Beware of bugs in the above code; I have only proved it correct, > not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas) Hi, 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. 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). 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. Also, please note that in my second example I ``un-merge'' stdout from stderr when I'm done. Thanks again, Richard Ryan