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

ruby-changes:59339

From: Nobuyoshi <ko1@a...>
Date: Fri, 20 Dec 2019 14:18:56 +0900 (JST)
Subject: [ruby-changes:59339] 435a4ca2a3 (master): Makes the receiver to FrozenError.new a keyword parameter

https://git.ruby-lang.org/ruby.git/commit/?id=435a4ca2a3

From 435a4ca2a38f3be1f5d2db0f71487a52c8285e9c Mon Sep 17 00:00:00 2001
From: Nobuyoshi Nakada <nobu@r...>
Date: Fri, 20 Dec 2019 14:14:07 +0900
Subject: Makes the receiver to FrozenError.new a keyword parameter

[Feature #16419]

diff --git a/error.c b/error.c
index e5dc0c3..b261f2e 100644
--- a/error.c
+++ b/error.c
@@ -1431,9 +1431,16 @@ exit_success_p(VALUE exc) https://github.com/ruby/ruby/blob/trunk/error.c#L1431
     return Qfalse;
 }
 
+static VALUE
+err_init_recv(VALUE exc, VALUE recv)
+{
+    if (recv != Qundef) rb_ivar_set(exc, id_recv, recv);
+    return exc;
+}
+
 /*
  * call-seq:
- *   FrozenError.new(msg=nil, receiver=nil)  -> frozen_error
+ *   FrozenError.new(msg=nil, receiver: nil)  -> frozen_error
  *
  * Construct a new FrozenError exception. If given the <i>receiver</i>
  * parameter may subsequently be examined using the FrozenError#receiver
@@ -1446,14 +1453,14 @@ exit_success_p(VALUE exc) https://github.com/ruby/ruby/blob/trunk/error.c#L1453
 static VALUE
 frozen_err_initialize(int argc, VALUE *argv, VALUE self)
 {
-    VALUE mesg, recv;
+    ID keywords[1];
+    VALUE values[numberof(keywords)], options;
 
-    argc = rb_scan_args(argc, argv, "02", &mesg, &recv);
-    if (argc > 1) {
-        argc--;
-        rb_ivar_set(self, id_recv, recv);
-    }
+    argc = rb_scan_args(argc, argv, "*:", NULL, &options);
+    keywords[0] = id_receiver;
+    rb_get_kwargs(options, keywords, 0, numberof(values), values);
     rb_call_super(argc, argv);
+    err_init_recv(self, values[0]);
     return self;
 }
 
@@ -1503,7 +1510,7 @@ name_err_init_attr(VALUE exc, VALUE recv, VALUE method) https://github.com/ruby/ruby/blob/trunk/error.c#L1510
     rb_control_frame_t *cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(ec->cfp);
     cfp = rb_vm_get_ruby_level_next_cfp(ec, cfp);
     rb_ivar_set(exc, id_name, method);
-    if (recv != Qundef) rb_ivar_set(exc, id_recv, recv);
+    err_init_recv(exc, recv);
     if (cfp) rb_ivar_set(exc, id_iseq, rb_iseqw_new(cfp->iseq));
     return exc;
 }
diff --git a/spec/ruby/core/exception/frozen_error_spec.rb b/spec/ruby/core/exception/frozen_error_spec.rb
index 7b35625..1b5ea71 100644
--- a/spec/ruby/core/exception/frozen_error_spec.rb
+++ b/spec/ruby/core/exception/frozen_error_spec.rb
@@ -12,7 +12,7 @@ describe "FrozenError.new" do https://github.com/ruby/ruby/blob/trunk/spec/ruby/core/exception/frozen_error_spec.rb#L12
   ruby_version_is "2.7" do
     it "should take optional receiver argument" do
       o = Object.new
-      FrozenError.new("msg", o).receiver.should equal(o)
+      FrozenError.new("msg", receiver: o).receiver.should equal(o)
     end
   end
 end
diff --git a/test/ruby/test_exception.rb b/test/ruby/test_exception.rb
index c7cfe81..94b59da 100644
--- a/test/ruby/test_exception.rb
+++ b/test/ruby/test_exception.rb
@@ -845,7 +845,7 @@ end.join https://github.com/ruby/ruby/blob/trunk/test/ruby/test_exception.rb#L845
 
   def test_frozen_error_initialize
     obj = Object.new
-    exc = FrozenError.new("bar", obj)
+    exc = FrozenError.new("bar", receiver: obj)
     assert_equal("bar", exc.message)
     assert_same(obj, exc.receiver)
 
-- 
cgit v0.10.2


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

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