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

ruby-changes:54364

From: mame <ko1@a...>
Date: Wed, 26 Dec 2018 23:42:14 +0900 (JST)
Subject: [ruby-changes:54364] mame:r66578 (trunk): Revert "string.c: remove the deprecation warnings of `String#bytes` with block"

mame	2018-12-26 23:42:07 +0900 (Wed, 26 Dec 2018)

  New Revision: 66578

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

  Log:
    Revert "string.c: remove the deprecation warnings of `String#bytes` with block"
    
    Forgot to write the ticket number in the commit log...

  Modified files:
    trunk/string.c
    trunk/test/ruby/test_string.rb
Index: string.c
===================================================================
--- string.c	(revision 66577)
+++ string.c	(revision 66578)
@@ -42,6 +42,8 @@ https://github.com/ruby/ruby/blob/trunk/string.c#L42
 # define HAVE_CRYPT_R 1
 #endif
 
+#define STRING_ENUMERATORS_WANTARRAY 0 /* next major */
+
 #undef rb_str_new
 #undef rb_usascii_str_new
 #undef rb_utf8_str_new
@@ -7954,7 +7956,22 @@ rb_str_split(VALUE str, const char *sep0 https://github.com/ruby/ruby/blob/trunk/string.c#L7956
     return rb_str_split_m(1, &sep, str);
 }
 
-#define WANTARRAY(m, size) (!rb_block_given_p() ? rb_ary_new_capa(size) : 0)
+static int
+enumerator_wantarray(const char *method)
+{
+    if (rb_block_given_p()) {
+#if STRING_ENUMERATORS_WANTARRAY
+	rb_warn("given block not used");
+#else
+	rb_warning("passing a block to String#%s is deprecated", method);
+	return 0;
+#endif
+    }
+    return 1;
+}
+
+#define WANTARRAY(m, size) \
+    (enumerator_wantarray(m) ? rb_ary_new_capa(size) : 0)
 
 static inline int
 enumerator_element(VALUE ary, VALUE e)
Index: test/ruby/test_string.rb
===================================================================
--- test/ruby/test_string.rb	(revision 66577)
+++ test/ruby/test_string.rb	(revision 66578)
@@ -878,15 +878,20 @@ CODE https://github.com/ruby/ruby/blob/trunk/test/ruby/test_string.rb#L878
         assert_equal [65, 66, 67], s.bytes {}
       }
     else
-      res = []
-      assert_equal s.object_id, s.bytes {|x| res << x }.object_id
-      assert_equal(65, res[0])
-      assert_equal(66, res[1])
-      assert_equal(67, res[2])
-      s = S("ABC")
-      res = []
-      assert_same s, s.bytes {|x| res << x }
-      assert_equal [65, 66, 67], res
+      warning = /passing a block to String#bytes is deprecated/
+      assert_warning(warning) {
+        res = []
+        assert_equal s.object_id, s.bytes {|x| res << x }.object_id
+        assert_equal(65, res[0])
+        assert_equal(66, res[1])
+        assert_equal(67, res[2])
+      }
+      assert_warning(warning) {
+        s = S("ABC")
+        res = []
+        assert_same s, s.bytes {|x| res << x }
+        assert_equal [65, 66, 67], res
+      }
     end
   end
 
@@ -917,15 +922,20 @@ CODE https://github.com/ruby/ruby/blob/trunk/test/ruby/test_string.rb#L922
         assert_equal [0x3042, 0x3044, 0x3046], s.codepoints {}
       }
     else
-      res = []
-      assert_equal s.object_id, s.codepoints {|x| res << x }.object_id
-      assert_equal(0x3042, res[0])
-      assert_equal(0x3044, res[1])
-      assert_equal(0x3046, res[2])
-      s = S("ABC")
-      res = []
-      assert_same s, s.codepoints {|x| res << x }
-      assert_equal [65, 66, 67], res
+      warning = /passing a block to String#codepoints is deprecated/
+      assert_warning(warning) {
+        res = []
+        assert_equal s.object_id, s.codepoints {|x| res << x }.object_id
+        assert_equal(0x3042, res[0])
+        assert_equal(0x3044, res[1])
+        assert_equal(0x3046, res[2])
+      }
+      assert_warning(warning) {
+        s = S("ABC")
+        res = []
+        assert_same s, s.codepoints {|x| res << x }
+        assert_equal [65, 66, 67], res
+      }
     end
   end
 
@@ -950,11 +960,14 @@ CODE https://github.com/ruby/ruby/blob/trunk/test/ruby/test_string.rb#L960
         assert_equal ["A", "B", "C"], s.chars {}
       }
     else
-      res = []
-      assert_equal s.object_id, s.chars {|x| res << x }.object_id
-      assert_equal("A", res[0])
-      assert_equal("B", res[1])
-      assert_equal("C", res[2])
+      warning = /passing a block to String#chars is deprecated/
+      assert_warning(warning) {
+        res = []
+        assert_equal s.object_id, s.chars {|x| res << x }.object_id
+        assert_equal("A", res[0])
+        assert_equal("B", res[1])
+        assert_equal("C", res[2])
+      }
     end
   end
 
@@ -1019,14 +1032,17 @@ CODE https://github.com/ruby/ruby/blob/trunk/test/ruby/test_string.rb#L1032
         assert_equal ["A", "B", "C"], "ABC".grapheme_clusters {}
       }
     else
-      s = "ABC".b.taint
-      res = []
-      assert_same s, s.grapheme_clusters {|x| res << x }
-      assert_equal(3, res.size)
-      assert_equal("A", res[0])
-      assert_equal("B", res[1])
-      assert_equal("C", res[2])
-      res.each {|g| assert_predicate(g, :tainted?)}
+      warning = /passing a block to String#grapheme_clusters is deprecated/
+      assert_warning(warning) {
+        s = "ABC".b.taint
+        res = []
+        assert_same s, s.grapheme_clusters {|x| res << x }
+        assert_equal(3, res.size)
+        assert_equal("A", res[0])
+        assert_equal("B", res[1])
+        assert_equal("C", res[2])
+        res.each {|g| assert_predicate(g, :tainted?)}
+      }
     end
   end
 
@@ -1139,10 +1155,12 @@ CODE https://github.com/ruby/ruby/blob/trunk/test/ruby/test_string.rb#L1155
         assert_equal ["hello\n", "world"], s.lines {}
       }
     else
-      res = []
-      assert_equal s.object_id, s.lines {|x| res << x }.object_id
-      assert_equal(S("hello\n"), res[0])
-      assert_equal(S("world"),  res[1])
+      assert_warning(/passing a block to String#lines is deprecated/) {
+        res = []
+        assert_equal s.object_id, s.lines {|x| res << x }.object_id
+        assert_equal(S("hello\n"), res[0])
+        assert_equal(S("world"),  res[1])
+      }
     end
   end
 

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

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