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

ruby-changes:67939

From: Shugo <ko1@a...>
Date: Sat, 11 Sep 2021 18:56:30 +0900 (JST)
Subject: [ruby-changes:67939] c60dbcd1c5 (master): Allow value omission in Hash literals

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

From c60dbcd1c55cd77a24c41d5e1a9555622be8b2b8 Mon Sep 17 00:00:00 2001
From: Shugo Maeda <shugo@r...>
Date: Sat, 11 Sep 2021 18:49:12 +0900
Subject: Allow value omission in Hash literals

`{x:, y:}` is a syntax sugar of `{x: x, y: y}`.
---
 parse.y                |  9 +++++++++
 test/ruby/test_hash.rb | 17 +++++++++++++++++
 2 files changed, 26 insertions(+)

diff --git a/parse.y b/parse.y
index 8017011..d4443d5 100644
--- a/parse.y
+++ b/parse.y
@@ -5617,6 +5617,15 @@ assoc		: arg_value tASSOC arg_value https://github.com/ruby/ruby/blob/trunk/parse.y#L5617
 		    /*% %*/
 		    /*% 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
 		    {
 		    /*%%%*/
diff --git a/test/ruby/test_hash.rb b/test/ruby/test_hash.rb
index f79879c..5aee1b5 100644
--- a/test/ruby/test_hash.rb
+++ b/test/ruby/test_hash.rb
@@ -2178,4 +2178,21 @@ class TestHash < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_hash.rb#L2178
       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
-- 
cgit v1.1


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

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