[前][次][番号順一覧][スレッド一覧]

ruby-changes:67192

From: Kazuki <ko1@a...>
Date: Thu, 19 Aug 2021 17:11:38 +0900 (JST)
Subject: [ruby-changes:67192] ecb6d6a4ef (master): Allow omission of parentheses in one line pattern matching [Feature #16182]

https://git.ruby-lang.org/ruby.git/commit/?id=ecb6d6a4ef

From ecb6d6a4ef058b5598a7633c3921eeab08ce11c6 Mon Sep 17 00:00:00 2001
From: Kazuki Tsujimoto <kazuki@c...>
Date: Thu, 19 Aug 2021 17:03:17 +0900
Subject: Allow omission of parentheses in one line pattern matching [Feature
 #16182]

---
 doc/syntax/pattern_matching.rdoc            |  8 +++++++-
 parse.y                                     |  4 ++--
 spec/ruby/language/pattern_matching_spec.rb | 11 +++++++++++
 test/ruby/test_pattern_matching.rb          | 14 +++++++-------
 4 files changed, 27 insertions(+), 10 deletions(-)

diff --git a/doc/syntax/pattern_matching.rdoc b/doc/syntax/pattern_matching.rdoc
index f80703d..b7d6147 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: https://github.com/ruby/ruby/blob/trunk/doc/syntax/pattern_matching.rdoc#L140
   end
   #=> "matched"
 
-In +case+ (but not in <code>=></code> 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 <code>=></code> and +in+) expressions, parentheses around https://github.com/ruby/ruby/blob/trunk/doc/syntax/pattern_matching.rdoc#L158
   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 cd69710..897b468 100644
--- a/parse.y
+++ b/parse.y
@@ -1732,7 +1732,7 @@ expr		: command_call https://github.com/ruby/ruby/blob/trunk/parse.y#L1732
 			p->ctxt.in_kwarg = 1;
 		    }
 		    {$<tbl>$ = push_pvtbl(p);}
-		  p_expr
+		  p_top_expr_body
 		    {pop_pvtbl(p, $<tbl>4);}
 		    {
 			p->ctxt.in_kwarg = $<ctxt>3.in_kwarg;
@@ -1750,7 +1750,7 @@ expr		: command_call https://github.com/ruby/ruby/blob/trunk/parse.y#L1750
 			p->ctxt.in_kwarg = 1;
 		    }
 		    {$<tbl>$ = push_pvtbl(p);}
-		  p_expr
+		  p_top_expr_body
 		    {pop_pvtbl(p, $<tbl>4);}
 		    {
 			p->ctxt.in_kwarg = $<ctxt>3.in_kwarg;
diff --git a/spec/ruby/language/pattern_matching_spec.rb b/spec/ruby/language/pattern_matching_spec.rb
index 4e9d42b..e9fc4f5 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 https://github.com/ruby/ruby/blob/trunk/spec/ruby/language/pattern_matching_spec.rb#L1135
         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 42b6802..277a0dc 100644
--- a/test/ruby/test_pattern_matching.rb
+++ b/test/ruby/test_pattern_matching.rb
@@ -1519,13 +1519,13 @@ END https://github.com/ruby/ruby/blob/trunk/test/ruby/test_pattern_matching.rb#L1519
     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)
-- 
cgit v1.1


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]