[前][次][番号順一覧][スレッド一覧]

ruby-changes:47027

From: glass <ko1@a...>
Date: Thu, 22 Jun 2017 15:50:39 +0900 (JST)
Subject: [ruby-changes:47027] glass:r59142 (trunk): Allow IO#reopen to take a block

glass	2017-06-22 15:50:32 +0900 (Thu, 22 Jun 2017)

  New Revision: 59142

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=59142

  Log:
    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

  Modified files:
    trunk/NEWS
    trunk/io.c
    trunk/test/ruby/test_io.rb
Index: test/ruby/test_io.rb
===================================================================
--- test/ruby/test_io.rb	(revision 59141)
+++ test/ruby/test_io.rb	(revision 59142)
@@ -2156,6 +2156,22 @@ class TestIO < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_io.rb#L2156
     }
   end
 
+  def test_reopen_with_block
+    make_tempfile {|t|
+      open(__FILE__) do |f|
+        f.gets
+        assert_nothing_raised {
+          reopened = nil
+          f.reopen(t.path) do |_reopened|
+            reopened = _reopened
+            assert_equal("foo\n", reopened.gets)
+          end
+          assert_equal(true, reopened.closed?)
+        }
+      end
+    }
+  end
+
   def test_reopen_inherit
     mkcdtmpdir {
       system(EnvUtil.rubybin, '-e', <<"End")
Index: io.c
===================================================================
--- io.c	(revision 59141)
+++ io.c	(revision 59142)
@@ -7058,6 +7058,10 @@ rb_io_reopen(int argc, VALUE *argv, VALU https://github.com/ruby/ruby/blob/trunk/io.c#L7058
 	}
     }
 
+    if (rb_block_given_p()) {
+	return rb_ensure(rb_yield, file, io_close, file);
+    }
+
     return file;
 }
 
Index: NEWS
===================================================================
--- NEWS	(revision 59141)
+++ NEWS	(revision 59142)
@@ -44,6 +44,7 @@ with all sufficient information, see the https://github.com/ruby/ruby/blob/trunk/NEWS#L44
 
   * IO#pread  [Feature #4532]
   * IO#pwrite  [Feature #4532]
+  * IO#reopen takes a block [Feature #2631]
 
 * IOError
 

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]