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

ruby-changes:42718

From: nobu <ko1@a...>
Date: Wed, 27 Apr 2016 14:51:20 +0900 (JST)
Subject: [ruby-changes:42718] nobu:r54792 (trunk): stringio.c: warn block for new

nobu	2016-04-27 15:47:56 +0900 (Wed, 27 Apr 2016)

  New Revision: 54792

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

  Log:
    stringio.c: warn block for new
    
    * ext/stringio/stringio.c (strio_s_new): warn if a block is given,
      as well as IO.new.

  Modified files:
    trunk/ChangeLog
    trunk/ext/stringio/stringio.c
    trunk/test/stringio/test_stringio.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 54791)
+++ ChangeLog	(revision 54792)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Wed Apr 27 15:47:54 2016  Nobuyoshi Nakada  <nobu@r...>
+
+	* ext/stringio/stringio.c (strio_s_new): warn if a block is given,
+	  as well as IO.new.
+
 Wed Apr 27 14:29:47 2016  Nobuyoshi Nakada  <nobu@r...>
 
 	* error.c (ruby_only_for_internal_use): raise fatal error when
Index: ext/stringio/stringio.c
===================================================================
--- ext/stringio/stringio.c	(revision 54791)
+++ ext/stringio/stringio.c	(revision 54792)
@@ -241,6 +241,19 @@ strio_s_open(int argc, VALUE *argv, VALU https://github.com/ruby/ruby/blob/trunk/ext/stringio/stringio.c#L241
     return rb_ensure(rb_yield, obj, strio_finalize, obj);
 }
 
+/* :nodoc: */
+static VALUE
+strio_s_new(int argc, VALUE *argv, VALUE klass)
+{
+    if (rb_block_given_p()) {
+	VALUE cname = rb_obj_as_string(klass);
+
+	rb_warn("%"PRIsVALUE"::new() does not take block; use %"PRIsVALUE"::open() instead",
+		cname, cname);
+    }
+    return rb_class_new_instance(argc, argv, klass);
+}
+
 /*
  * Returns +false+.  Just for compatibility to IO.
  */
@@ -1523,6 +1536,7 @@ Init_stringio(void) https://github.com/ruby/ruby/blob/trunk/ext/stringio/stringio.c#L1536
 
     rb_include_module(StringIO, rb_mEnumerable);
     rb_define_alloc_func(StringIO, strio_s_allocate);
+    rb_define_singleton_method(StringIO, "new", strio_s_new, -1);
     rb_define_singleton_method(StringIO, "open", strio_s_open, -1);
     rb_define_method(StringIO, "initialize", strio_initialize, -1);
     rb_define_method(StringIO, "initialize_copy", strio_copy, 1);
Index: test/stringio/test_stringio.rb
===================================================================
--- test/stringio/test_stringio.rb	(revision 54791)
+++ test/stringio/test_stringio.rb	(revision 54792)
@@ -674,4 +674,10 @@ class TestStringIO < Test::Unit::TestCas https://github.com/ruby/ruby/blob/trunk/test/stringio/test_stringio.rb#L674
     bug_11945 = '[ruby-core:72699] [Bug #11945]'
     assert_equal Encoding::ASCII_8BIT, s.external_encoding, bug_11945
   end
+
+  def test_new_block_warning
+    assert_warn(/does not take block/) do
+      StringIO.new {}
+    end
+  end
 end

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

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