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

ruby-changes:63185

From: Jeremy <ko1@a...>
Date: Tue, 29 Sep 2020 00:38:22 +0900 (JST)
Subject: [ruby-changes:63185] 92c3ad9c27 (master): Make Warning.warn accept only category keyword

https://git.ruby-lang.org/ruby.git/commit/?id=92c3ad9c27

From 92c3ad9c276d048bf6fba1145ffccf8678fe8af1 Mon Sep 17 00:00:00 2001
From: Jeremy Evans <code@j...>
Date: Wed, 2 Sep 2020 15:49:40 -0700
Subject: Make Warning.warn accept only category keyword

In general accepting arbitrary keywords is a bad idea unless you are
delegating keywords or acting on arbitrary keywords.  In this case,
the category keyword is ignored, and it's less error prone to not
ignore all keywords.

diff --git a/error.c b/error.c
index 9f8cdf8..e064eef 100644
--- a/error.c
+++ b/error.c
@@ -72,6 +72,7 @@ static VALUE rb_mWarning; https://github.com/ruby/ruby/blob/trunk/error.c#L72
 static VALUE rb_cWarningBuffer;
 
 static ID id_warn;
+static ID id_category;
 
 extern const char ruby_description[];
 
@@ -231,11 +232,11 @@ rb_warning_s_aset(VALUE mod, VALUE category, VALUE flag) https://github.com/ruby/ruby/blob/trunk/error.c#L232
 
 /*
  * call-seq:
- *    warn(msg, **kw)  -> nil
+ *    warn(msg, category: nil)  -> nil
  *
  * Writes warning message +msg+ to $stderr. This method is called by
  * Ruby for all emitted warnings. A +category+ may be included with
- * the warning.
+ * the warning, but is ignored by default.
  */
 
 static VALUE
@@ -243,8 +244,11 @@ rb_warning_s_warn(int argc, VALUE *argv, VALUE mod) https://github.com/ruby/ruby/blob/trunk/error.c#L244
 {
     VALUE str;
     VALUE opt;
+    VALUE category;
 
     rb_scan_args(argc, argv, "1:", &str, &opt);
+    if (!NIL_P(opt)) rb_get_kwargs(opt, &id_category, 0, 1, &category);
+
     Check_Type(str, T_STRING);
     rb_must_asciicompat(str);
     rb_write_error_str(str);
@@ -2749,6 +2753,7 @@ Init_Exception(void) https://github.com/ruby/ruby/blob/trunk/error.c#L2753
     id_errno = rb_intern_const("errno");
     id_i_path = rb_intern_const("@path");
     id_warn = rb_intern_const("warn");
+    id_category = rb_intern_const("category");
     id_top = rb_intern_const("top");
     id_bottom = rb_intern_const("bottom");
     id_iseq = rb_make_internal_id();
diff --git a/test/ruby/test_exception.rb b/test/ruby/test_exception.rb
index 120b041..0333fd5 100644
--- a/test/ruby/test_exception.rb
+++ b/test/ruby/test_exception.rb
@@ -925,8 +925,8 @@ $stderr = $stdout; raise "\x82\xa0"') do |outs, errs, status| https://github.com/ruby/ruby/blob/trunk/test/ruby/test_exception.rb#L925
       remove_method :warn
 
       if category
-        define_method(:warn) do |str, **kw|
-          warning << [str, kw[:category]]
+        define_method(:warn) do |str, category: nil|
+          warning << [str, category]
         end
       else
         define_method(:warn) do |str|
-- 
cgit v0.10.2


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

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