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

ruby-changes:67944

From: Shugo <ko1@a...>
Date: Sat, 11 Sep 2021 20:25:16 +0900 (JST)
Subject: [ruby-changes:67944] 297f9b8d4c (master): Add documentation and tests for keyword argument value omission

https://git.ruby-lang.org/ruby.git/commit/?id=297f9b8d4c

From 297f9b8d4c4502aa2ba0eccf93dfce215a7b6dfe Mon Sep 17 00:00:00 2001
From: Shugo Maeda <shugo@r...>
Date: Sat, 11 Sep 2021 20:23:36 +0900
Subject: Add documentation and tests for keyword argument value omission

[Feature #14579]
---
 NEWS.md                   |  4 ++--
 test/ruby/test_hash.rb    | 18 ------------------
 test/ruby/test_keyword.rb | 16 ++++++++++++++++
 test/ruby/test_literal.rb | 16 ++++++++++++++++
 4 files changed, 34 insertions(+), 20 deletions(-)

diff --git a/NEWS.md b/NEWS.md
index 6c5c0d4..666f857 100644
--- a/NEWS.md
+++ b/NEWS.md
@@ -69,10 +69,10 @@ Note that each entry is kept to a minimum, see links for details. https://github.com/ruby/ruby/blob/trunk/NEWS.md#L69
 
     [[Bug #4443]]
 
-* Values in Hash literals can be omitted.  [[Feature #14579]]
+* Values in Hash literals and keyword arguments can be omitted.  [[Feature #14579]]
 
     `{x:, y:}` is a syntax sugar of `{x: x, y: y}`.
-
+    `foo(x:, y:)` is a syntax sugar of `foo(x: x, y: y)`.
 
 ## Command line options
 
diff --git a/test/ruby/test_hash.rb b/test/ruby/test_hash.rb
index af78e2f..f79879c 100644
--- a/test/ruby/test_hash.rb
+++ b/test/ruby/test_hash.rb
@@ -2178,22 +2178,4 @@ class TestHash < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_hash.rb#L2178
       end;
     end
   end
-
-  def test_value_omission
-    x = 1
-    y = 2
-    assert_equal({x: 1, y: 2}, {x:, y:})
-    assert_equal({one: 1, two: 2}, {one:, two:})
-    assert_syntax_error('{"#{x}":}', /'\}'/)
-  end
-
-  private
-
-  def one
-    1
-  end
-
-  def two
-    2
-  end
 end
diff --git a/test/ruby/test_keyword.rb b/test/ruby/test_keyword.rb
index c8191a7..a899934 100644
--- a/test/ruby/test_keyword.rb
+++ b/test/ruby/test_keyword.rb
@@ -4351,4 +4351,20 @@ class TestKeywordArgumentsSymProcRefinements < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_keyword.rb#L4351
     assert_raise(TypeError, bug16603) { p(**42) }
     assert_raise(TypeError, bug16603) { p(k:1, **42) }
   end
+
+  def test_value_omission
+    f = ->(**kwargs) { kwargs }
+    x = 1
+    y = 2
+    assert_equal({x: 1, y: 2}, f.call(x:, y:))
+    assert_equal({one: 1, two: 2}, f.call(one:, two:))
+  end
+
+  private def one
+    1
+  end
+
+  private def two
+    2
+  end
 end
diff --git a/test/ruby/test_literal.rb b/test/ruby/test_literal.rb
index 579a39d..c8a65c9 100644
--- a/test/ruby/test_literal.rb
+++ b/test/ruby/test_literal.rb
@@ -506,6 +506,22 @@ class TestRubyLiteral < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_literal.rb#L506
     assert_equal(100, h['a'])
   end
 
+  def test_hash_value_omission
+    x = 1
+    y = 2
+    assert_equal({x: 1, y: 2}, {x:, y:})
+    assert_equal({one: 1, two: 2}, {one:, two:})
+    assert_syntax_error('{"#{x}":}', /'\}'/)
+  end
+
+  private def one
+    1
+  end
+
+  private def two
+    2
+  end
+
   def test_range
     assert_instance_of Range, (1..2)
     assert_equal(1..2, 1..2)
-- 
cgit v1.1


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

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