Reopening a stream on a File object

io, ruby

Solution

`IO#close` documentation says:

close → nil

Closes ios and flushes any pending writes to the operating system. The stream is unavailable for any further data operations; an IOError is raised if such an attempt is made.

This means that you cannot reopen the same stream again, as it probably has closed the underlying file descriptor also by that time.

What `reopen` gives you, is that you can make this object point to another stream/file. Can be useful if your IO object is bound to a constant, but you need to close the file and reopen another.

Problem

Say you close a ruby `File` object ``` f = File.open('testfile') f.close f.each_byte { ... } #=> IOError: closed stream ``` I know you can reopen the stream through `f = File.open(f)`, but is there a way to reopen the stream through an `IO` method on the object? e.g. ``` f.close f.open_again f.each_byte { ... } ```

Original source