Project

General

Profile

Actions

Feature #2631

open

Allow IO#reopen to take a block

Added by djberg96 (Daniel Berger) about 14 years ago. Updated almost 6 years ago.

Status:
Assigned
Target version:
-
[ruby-core:27701]

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

Actions #1

Updated by hongli (Hongli Lai) about 14 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

Actions #2

Updated by matz (Yukihiro Matsumoto) about 14 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 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.
Actions #3

Updated by shyouhei (Shyouhei Urabe) about 14 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.

Actions #4

Updated by djberg96 (Daniel Berger) about 14 years ago

On Fri, Jan 22, 2010 at 10:56 PM, Shyouhei Urabe 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.

Actions #5

Updated by znz (Kazuhiro NISHIYAMA) almost 14 years ago

  • Category set to core
  • Target version set to 2.0.0

Updated by nahi (Hiroshi Nakamura) about 12 years ago

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

Method name?

Updated by akr (Akira Tanaka) about 12 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.

Actions #8

Updated by shyouhei (Shyouhei Urabe) about 12 years ago

  • Status changed from Open to Assigned

Updated by shyouhei (Shyouhei Urabe) over 11 years ago

  • Target version changed from 2.0.0 to 2.6

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?

Actions #10

Updated by Anonymous over 6 years ago

  • Status changed from Assigned to Closed

Applied in changeset trunk|r59142.


Allow IO#reopen to take a block

  • io.c (rb_io_reopen): take a block and ensure the IO closed
    [Feature #2631]

  • test/ruby/test_io.rb: add a test

  • NEWS: add an entry for this change

Updated by naruse (Yui NARUSE) over 6 years ago

  • Status changed from Closed to Open

匿名ユーザー wrote:

Applied in changeset trunk|r59142.


Allow IO#reopen to take a block

  • io.c (rb_io_reopen): take a block and ensure the IO closed
    [Feature #2631]

  • test/ruby/test_io.rb: add a test

  • NEWS: add an entry for this change

This patch looks doesn't recover the original $stdout.

Morever though this was approved before, this is strange because block changes global state.
If this is introduced in 7 years ago, it may be acceptable.
But in this 201x year Ruby really should support multithread unsafe code?
People who want to write unsafe code should explicitly write code like following.

require 'mkmf'

# stdout redirected within block only
orig_stdout = $stdout
open(IO:NULL, 'w') do |f|
  $stdout = f
  if have_header('foo.h')
    # Do stuff
  end
ensure
  $stdout = orig_stdout
end
Actions #12

Updated by naruse (Yui NARUSE) over 6 years ago

  • Target version changed from 2.6 to 2.5

Updated by Glass_saga (Masaki Matsushita) over 6 years ago

  • Status changed from Open to Closed
  • Assignee changed from shyouhei (Shyouhei Urabe) to Glass_saga (Masaki Matsushita)
Actions #14

Updated by Glass_saga (Masaki Matsushita) over 6 years ago

  • Status changed from Closed to Assigned

Updated by Glass_saga (Masaki Matsushita) over 6 years ago

It was thoughtless. Let me revert it.

Actions #16

Updated by naruse (Yui NARUSE) over 6 years ago

  • Target version deleted (2.5)

Updated by Bookgoogleflights (google flights) almost 6 years ago

The idea of temporary intercepting any output to another IO is worth considering. Though I doubt to name that feature a "reopen".est/ruby/test_io.rb: add a test NEWS: add an entry for this change, he new Google Flights offers all of the speed and usability of the old Google Flights, but with some great new features added on top book flights using "bookgoogleflights.wordpress.com":https://bookgoogleflights.wordpress.com/

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0