From: Joel VanderWerf Date: 2005-02-25T07:19:12+09:00 Subject: Re: How to extend $stdout#write for system calls? Jim Freeze wrote: > Hi > > I am writing a tee function for ruby and am pretty close. > The problem I am having now is that I don't know > how to extend $stdout#write and have it survive > a system (or exec) call. > > Below is some code illustrating what I have: > > > cat tee.rb > module M > def write(a) > super("fred\n") > end > end > > $stdout.extend(M) > puts "puts: should be fred" # prints twice for some reason > system("echo", "system: should be fred") # no fred > > > ruby tee.rb > fred > fred > system: should be fred > > If this is not possible, maybe there is a way with alias_method, > but I haven't been able to get that going either. How could this work if echo is not even a ruby program? Maybe if you run echo with popen, and print the output of that... But if you want it to work with arb. ruby code, this won't help. Probably the only hope is to $stdout.reopen into something you can read and do what you want with. Never tried it tho.