Feature #2631
openAllow IO#reopen to take a block
Added by djberg96 (Daniel Berger) almost 15 years ago. Updated over 6 years ago.
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
Updated by hongli (Hongli Lai) almost 15 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
Updated by matz (Yukihiro Matsumoto) almost 15 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) almost 15 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) almost 15 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) over 14 years ago
- Category set to core
- Target version set to 2.0.0
Updated by nahi (Hiroshi Nakamura) over 12 years ago
- Description updated (diff)
- Assignee set to shyouhei (Shyouhei Urabe)
Method name?
Updated by akr (Akira Tanaka) over 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.
Updated by shyouhei (Shyouhei Urabe) over 12 years ago
- Status changed from Open to Assigned
Updated by shyouhei (Shyouhei Urabe) about 12 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?
Updated by Anonymous over 7 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 7 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
Updated by naruse (Yui NARUSE) over 7 years ago
- Target version changed from 2.6 to 2.5
Updated by Glass_saga (Masaki Matsushita) over 7 years ago
- Status changed from Open to Closed
- Assignee changed from shyouhei (Shyouhei Urabe) to Glass_saga (Masaki Matsushita)
Updated by Glass_saga (Masaki Matsushita) over 7 years ago
- Status changed from Closed to Assigned
Updated by Glass_saga (Masaki Matsushita) over 7 years ago
It was thoughtless. Let me revert it.
Updated by Bookgoogleflights (google flights) over 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/