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

Allow value omission in Hash literals

`{x:, y:}` is a syntax sugar of `{x: x, y: y}`.
This commit is contained in:
Shugo Maeda 2021-09-11 18:49:12 +09:00
parent 64e056a4c5
commit c60dbcd1c5
No known key found for this signature in database
GPG key ID: 2DFE34085E97CE47
2 changed files with 26 additions and 0 deletions

View file

@ -5617,6 +5617,15 @@ assoc : arg_value tASSOC arg_value
/*% %*/
/*% ripper: assoc_new!($1, $2) %*/
}
| tLABEL
{
/*%%%*/
NODE *val = gettable(p, $1, &@$);
if (!val) val = NEW_BEGIN(0, &@$);
$$ = list_append(p, NEW_LIST(NEW_LIT(ID2SYM($1), &@1), &@$), val);
/*% %*/
/*% ripper: assoc_new!($1, id_is_var(p, get_id($1)) ? var_ref!($1) : vcall!($1)) %*/
}
| tSTRING_BEG string_contents tLABEL_END arg_value
{
/*%%%*/

View file

@ -2178,4 +2178,21 @@ 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:})
end
private
def one
1
end
def two
2
end
end