Feature #2631

Allow IO#reopen to take a block

Added by Daniel Berger about 4 years ago. Updated over 1 year ago.

[ruby-core:27701]
Status:Assigned
Priority:Normal
Assignee:Shyouhei Urabe
Category:core
Target version:next minor

Description

=begin
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
=end

History

#1 Updated by Hongli Lai about 4 years ago

=begin
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.
=end

#2 Updated by Yukihiro Matsumoto about 4 years ago

=begin
Hi,

In message "Re: [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.

=end

#3 Updated by Shyouhei Urabe about 4 years ago

=begin
+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.
=end

#4 Updated by Daniel Berger about 4 years ago

=begin
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.

=end

#5 Updated by Kazuhiro NISHIYAMA about 4 years ago

  • Category set to core
  • Target version set to 2.0.0

=begin

=end

#6 Updated by Hiroshi Nakamura about 2 years ago

  • Description updated (diff)
  • Assignee set to Shyouhei Urabe

Method name?

#7 Updated by Akira Tanaka about 2 years 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.

#8 Updated by Shyouhei Urabe about 2 years ago

  • Status changed from Open to Assigned

#9 Updated by Shyouhei Urabe over 1 year ago

  • Target version changed from 2.0.0 to next minor

I was poked by _ko1. But we lack a implementation proposal.

So I move its target to next minor. It might happen to be implemented some time later but not today.

Any opinions?

Also available in: Atom PDF