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:
parent
d05ef38865
commit
297f9b8d4c
4 changed files with 34 additions and 20 deletions
4
NEWS.md
4
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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Add table
Reference in a new issue