Feature #2631

Allow IO#reopen to take a block

Added by djberg96 (Daniel Berger) over 2 years ago. Updated 2 months ago.

[ruby-core:27701]
Status:Assigned Start date:01/23/2010
Priority:Normal Due date:
Assignee:shyouhei (Shyouhei Urabe) % Done:

0%

Category:core
Target version:2.0.0

Description

Please allow IO#reopen to accept a block. This would allow users to temporarily redirect output without having to manually reset the file descriptor. For example:

require 'mkmf'

# stdout redirected within block only
$stdout.reopen('/dev/null') do
  if have_header('foo.h')
    # Do stuff
  end
end

# stdout now back to its former setting

I believe this is both convenient and intuitive when one considers the IO.open also takes a block.

Regards,

Dan

History

Updated by hongli (Hongli Lai) over 2 years ago

I don't think this can be implemented easily. This can be implemented if the IO object in question is a File for which you know the filename. For sockets, pipes and other stuff it becomes tricker: one can emulate it backing up the underlying file descriptor by dup()ing it, but in my opinion that goes against my expectation of what #reopen should do: to replace the file descriptor with something else.

Updated by matz (Yukihiro Matsumoto) over 2 years ago

Hi,

In message "Re: [ruby-core:27702] [Feature #2631] Allow IO#reopen to take a block"
    on Sat, 23 Jan 2010 01:30:53 +0900, Hongli Lai <redmine@ruby-lang.org> writes:

|I don't think this can be implemented easily. This can be implemented if the IO object in question is a File for which you know the filename. For sockets, pipes and other stuff it becomes tricker: one can emulate it backing up the underlying file descriptor by dup()ing it, but in my opinion that goes against my expectation of what #reopen should do: to replace the file descriptor with something else.

Agreed.  Besides that behavior would not be thread safe.

							matz.

Updated by shyouhei (Shyouhei Urabe) over 2 years ago

+1, The idea of temporary intercepting any output to another IO is worth considering.  Though I doubt to name that feature a "reopen".

So my suggestion is: create a new method, that takes a block, to intercept an IO.

Updated by djberg96 (Daniel Berger) over 2 years ago

On Fri, Jan 22, 2010 at 10:56 PM, Shyouhei Urabe <redmine@ruby-lang.org> wrote:
> Issue #2631 has been updated by Shyouhei Urabe.
>
>
> +1, The idea of temporary intercepting any output to another IO is worth considering.  Though I doubt to name that feature a "reopen".

How about IO#redirect.

Updated by znz (Kazuhiro NISHIYAMA) about 2 years ago

  • Category set to core
  • Target version set to 2.0.0

Updated by nahi (Hiroshi Nakamura) 2 months ago

  • Description updated (diff)
  • Assignee set to shyouhei (Shyouhei Urabe)
Method name?

Updated by akr (Akira Tanaka) 2 months ago

I don't feel changing IO#reopen is great way to solve this issue. How about spawn()? Since Ruby 1.9, spawn provides a primitive for redirection. It is thread safe. Apart from that, I think $stdin, $stdout, $stderr can be thread-local variables, though.

Updated by shyouhei (Shyouhei Urabe) 2 months ago

  • Status changed from Open to Assigned

Also available in: Atom PDF