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

ruby-changes:58955

From: Yusuke <ko1@a...>
Date: Fri, 29 Nov 2019 17:07:49 +0900 (JST)
Subject: [ruby-changes:58955] 3a87826d0c (master): vm_method.c: add top-level ruby2_keywords

https://git.ruby-lang.org/ruby.git/commit/?id=3a87826d0c

From 3a87826d0c3dd4c42e327e0cd4fb0806d898497f Mon Sep 17 00:00:00 2001
From: Yusuke Endoh <mame@r...>
Date: Fri, 29 Nov 2019 16:51:13 +0900
Subject: vm_method.c: add top-level ruby2_keywords

This is a top-level version of Module#ruby2_keywords.
It can be used for functions (top-level methods) that delegates
arguments.  [Feature #16364]

diff --git a/test/ruby/test_keyword.rb b/test/ruby/test_keyword.rb
index 29335a4..862b673 100644
--- a/test/ruby/test_keyword.rb
+++ b/test/ruby/test_keyword.rb
@@ -2985,6 +2985,18 @@ class TestKeywordArguments < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_keyword.rb#L2985
     assert_raise(FrozenError) { c.send(:ruby2_keywords, :baz) }
   end
 
+  def test_top_ruby2_keywords
+    assert_in_out_err([], <<-INPUT, ["[1, 2, 3]", "{:k=>1}"], [])
+      def bar(*a, **kw)
+        p a, kw
+      end
+      ruby2_keywords def foo(*a)
+        bar(*a)
+      end
+      foo(1, 2, 3, k:1)
+    INPUT
+  end
+
   def test_dig_kwsplat
     kw = {}
     h = {:a=>1}
diff --git a/vm_method.c b/vm_method.c
index 9b34ce1..1a6b1cf 100644
--- a/vm_method.c
+++ b/vm_method.c
@@ -1949,6 +1949,19 @@ top_private(int argc, VALUE *argv, VALUE _) https://github.com/ruby/ruby/blob/trunk/vm_method.c#L1949
 
 /*
  *  call-seq:
+ *     ruby2_keywords(method_name, ...) -> self
+ *
+ *  For the given method names, marks the method as passing keywords through
+ *  a normal argument splat.  See Module#ruby2_keywords in detail.
+ */
+static VALUE
+top_ruby2_keywords(int argc, VALUE *argv, VALUE module)
+{
+    return rb_mod_ruby2_keywords(argc, argv, rb_cObject);
+}
+
+/*
+ *  call-seq:
  *     module_function(symbol, ...)    -> self
  *     module_function(string, ...)    -> self
  *
@@ -2280,6 +2293,8 @@ Init_eval_method(void) https://github.com/ruby/ruby/blob/trunk/vm_method.c#L2293
 			     "public", top_public, -1);
     rb_define_private_method(rb_singleton_class(rb_vm_top_self()),
 			     "private", top_private, -1);
+    rb_define_private_method(rb_singleton_class(rb_vm_top_self()),
+			     "ruby2_keywords", top_ruby2_keywords, -1);
 
     {
 #define REPLICATE_METHOD(klass, id) do { \
-- 
cgit v0.10.2


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

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