summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShugo Maeda <shugo@ruby-lang.org>2021-09-11 20:23:36 +0900
committerShugo Maeda <shugo@ruby-lang.org>2021-09-11 20:23:36 +0900
commit297f9b8d4c4502aa2ba0eccf93dfce215a7b6dfe (patch)
tree0b47c4b68336f12c62187ec3ce612934f4f289c2
parentd05ef38865af8dd43fd583e46f46509fe29e93cb (diff)
Add documentation and tests for keyword argument value omission
[Feature #14579]
-rw-r--r--NEWS.md4
-rw-r--r--test/ruby/test_hash.rb18
-rw-r--r--test/ruby/test_keyword.rb16
-rw-r--r--test/ruby/test_literal.rb16
4 files changed, 34 insertions, 20 deletions
diff --git a/NEWS.md b/NEWS.md
index 6c5c0d4e3c..666f857c49 100644
--- a/NEWS.md
+++ b/NEWS.md
@@ -69,10 +69,10 @@ Note that each entry is kept to a minimum, see links for details.
[[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 af78e2fd29..f79879c20a 100644
--- a/test/ruby/test_hash.rb
+++ b/test/ruby/test_hash.rb
@@ -2178,22 +2178,4 @@ class TestHash < Test::Unit::TestCase
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 c8191a722c..a899934ed6 100644
--- a/test/ruby/test_keyword.rb
+++ b/test/ruby/test_keyword.rb
@@ -4351,4 +4351,20 @@ class TestKeywordArgumentsSymProcRefinements < Test::Unit::TestCase
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 579a39dee8..c8a65c923a 100644
--- a/test/ruby/test_literal.rb
+++ b/test/ruby/test_literal.rb
@@ -506,6 +506,22 @@ class TestRubyLiteral < Test::Unit::TestCase
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)