diff --git a/doc/syntax/pattern_matching.rdoc b/doc/syntax/pattern_matching.rdoc
index f80703d5c6..b7d614770c 100644
--- a/doc/syntax/pattern_matching.rdoc
+++ b/doc/syntax/pattern_matching.rdoc
@@ -140,7 +140,7 @@ Both array and hash patterns support "rest" specification:
end
#=> "matched"
-In +case+ (but not in =>
and +in+) expressions, parentheses around both kinds of patterns could be omitted:
+Parentheses around both kinds of patterns could be omitted:
case [1, 2]
in Integer, Integer
@@ -158,6 +158,12 @@ In +case+ (but not in =>
and +in+) expressions, parentheses around
end
#=> "matched"
+ [1, 2] => a, b
+ [1, 2] in a, b
+
+ {a: 1, b: 2, c: 3} => a:
+ {a: 1, b: 2, c: 3} in a:
+
Find pattern is similar to array pattern but it can be used to check if the given object has any elements that match the pattern:
case ["a", 1, "b", "c", 2]
diff --git a/parse.y b/parse.y
index cd6971055a..897b46838e 100644
--- a/parse.y
+++ b/parse.y
@@ -1732,7 +1732,7 @@ expr : command_call
p->ctxt.in_kwarg = 1;
}
{$$ = push_pvtbl(p);}
- p_expr
+ p_top_expr_body
{pop_pvtbl(p, $4);}
{
p->ctxt.in_kwarg = $3.in_kwarg;
@@ -1750,7 +1750,7 @@ expr : command_call
p->ctxt.in_kwarg = 1;
}
{$$ = push_pvtbl(p);}
- p_expr
+ p_top_expr_body
{pop_pvtbl(p, $4);}
{
p->ctxt.in_kwarg = $3.in_kwarg;
diff --git a/spec/ruby/language/pattern_matching_spec.rb b/spec/ruby/language/pattern_matching_spec.rb
index 4e9d42b257..e9fc4f557d 100644
--- a/spec/ruby/language/pattern_matching_spec.rb
+++ b/spec/ruby/language/pattern_matching_spec.rb
@@ -1135,5 +1135,16 @@ ruby_version_is "2.7" do
result.should == true
end
end
+
+ ruby_version_is "3.1" do
+ it "can omit parentheses in one line pattern matching" do
+ [1, 2] => a, b
+ a.should == 1
+ b.should == 2
+
+ {a: 1} => a:
+ a.should == 1
+ end
+ end
end
end
diff --git a/test/ruby/test_pattern_matching.rb b/test/ruby/test_pattern_matching.rb
index 42b6802fe6..277a0dcc51 100644
--- a/test/ruby/test_pattern_matching.rb
+++ b/test/ruby/test_pattern_matching.rb
@@ -1519,13 +1519,13 @@ END
assert_raise(NoMatchingPatternError) do
{a: 1} => {a: 0}
end
- assert_syntax_error("if {} => {a:}; end", /void value expression/)
- assert_syntax_error(%q{
- 1 => a, b
- }, /unexpected/, '[ruby-core:95098]')
- assert_syntax_error(%q{
- 1 => a:
- }, /unexpected/, '[ruby-core:95098]')
+
+ [1, 2] => a, b
+ assert_equal 1, a
+ assert_equal 2, b
+
+ {a: 1} => a:
+ assert_equal 1, a
assert_equal true, (1 in 1)
assert_equal false, (1 in 2)