1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Add documentation and tests for keyword argument value omission

[Feature #14579]
This commit is contained in:
Shugo Maeda 2021-09-11 20:23:36 +09:00
parent d05ef38865
commit 297f9b8d4c
No known key found for this signature in database
GPG key ID: 2DFE34085E97CE47
4 changed files with 34 additions and 20 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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)