Thomas Søîdergaard <tsondergaard / speakanet.com> graced us by uttering: > Is there a sensible way to write the contents of one stream > to another? For those with memory to spare: ostream.write(istream.read) For others: ostream.puts(istream.readlines) (Does IO#puts call itself on each element of a passed array? Or does it convert it as a whole? IOW, ....is it (pseudocode) def puts(arr) arr.each { |item| self.puts(item) } end ....or is it def puts(arr) str = "" arr.each { |i| str += i.to_s } self.puts(str) end ???) [ snip ] > No one has written a reply to this question! I expect this means > that there is no nice way to do it. I plan to submit an RCR about > this. What I essentially want is a mechanism for writing an input > stream to an output stream, so I don't have to create a suitable > buffer and read, write, read, write myself. > Which of the following options appeal more to you: > > 1) ostream << istream works differently for istream than for any other > object. instead of writing to_s to the stream it writes the contents > of the istream. A quick hack could define IO#to_s to return the contents of the file. class IO def to_s return self.read end end OTOH, this will raise an IOError on subsequent direct or indirect calls. This is configurable, but in the end this will probably be more trouble than it's worth, as you allude to below. > 2) ostream.writeStream(istream) is added as a separate method. > > 3) ostream << anObject invokes anObject.writeToStream(ostream). > > class Object def writeToStream(anOStream) anOStream << to_s end end > > class IO def writeToStream(anOStream) // override Object impl. and > write this object to anOStream end end I see no reason to rework the entire class hierarchy just for a little syntactic sugar... > I like 3, except that if anIO2 in 'anIO1 << anIO2' is not open for > input it doesn't make a lot of sense. More evidence to the point that > IO is a little bit messy, I think. Not necessarily. IMHO, IO as it sits is a more general purpose interface. If you have special needs, feel free to subclass IO and overload #to_s. That's what it's for. > What do you think? I would probably just use: # shows my Perl roots ostream.print while istream.gets or # a bit more Rubyish istream.each { |line| ostream.write line } HTH Tim Hammerquist -- Hell doesn't always look like Hell. On a good day it can look a lot like L.A. -- David Duchovny, "Playing God"