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

ruby-changes:70328

From: Victor <ko1@a...>
Date: Mon, 20 Dec 2021 01:10:04 +0900 (JST)
Subject: [ruby-changes:70328] a0f10a973f (master): [DOC] Add documentation for hash value omission syntax

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

From a0f10a973fb94a0ee73da7cab792128cdf601783 Mon Sep 17 00:00:00 2001
From: Victor Shepelev <zverok.offline@g...>
Date: Sun, 19 Dec 2021 18:09:52 +0200
Subject: [DOC] Add documentation for hash value omission syntax

---
 doc/syntax/calling_methods.rdoc | 18 ++++++++++++++++++
 doc/syntax/literals.rdoc        |  8 ++++++++
 hash.c                          |  8 ++++++++
 3 files changed, 34 insertions(+)

diff --git a/doc/syntax/calling_methods.rdoc b/doc/syntax/calling_methods.rdoc
index fc806d5c311..da061dbfdb4 100644
--- a/doc/syntax/calling_methods.rdoc
+++ b/doc/syntax/calling_methods.rdoc
@@ -210,6 +210,24 @@ definition.  If a keyword argument is given that the method did not list, https://github.com/ruby/ruby/blob/trunk/doc/syntax/calling_methods.rdoc#L210
 and the method definition does not accept arbitrary keyword arguments, an
 ArgumentError will be raised.
 
+Keyword argument value can be omitted, meaning the value will be be fetched
+from the context by the name of the key
+
+  keyword1 = 'some value'
+  my_method(positional1, keyword1:)
+  # ...is the same as
+  my_method(positional1, keyword1: keyword1)
+
+Be aware that when method parenthesis are omitted, too, the parsing order might
+be unexpected:
+
+  my_method positional1, keyword1:
+
+  some_other_expression
+
+  # ...is actually parsed as
+  my_method(positional1, keyword1: some_other_expression)
+
 === Block Argument
 
 The block argument sends a closure from the calling scope to the method.
diff --git a/doc/syntax/literals.rdoc b/doc/syntax/literals.rdoc
index 66e17fd5034..32fe5110ce0 100644
--- a/doc/syntax/literals.rdoc
+++ b/doc/syntax/literals.rdoc
@@ -356,6 +356,14 @@ is equal to https://github.com/ruby/ruby/blob/trunk/doc/syntax/literals.rdoc#L356
 
   { :"a 1" => 1, :"b 2" => 2 }
 
+Hash values can be omitted, meaning that value will be fetched from the context
+by the name of the key:
+
+  x = 100
+  y = 200
+  h = { x:, y: }
+  #=> {:x=>100, :y=>200}
+
 See Hash for the methods you may use with a hash.
 
 == \Range Literals
diff --git a/hash.c b/hash.c
index 1155a5756b5..07ec93d7840 100644
--- a/hash.c
+++ b/hash.c
@@ -6701,6 +6701,14 @@ static const rb_data_type_t env_data_type = { https://github.com/ruby/ruby/blob/trunk/hash.c#L6701
  *    # Raises SyntaxError (syntax error, unexpected ':', expecting =>):
  *    h = {0: 'zero'}
  *
+ *  Hash value can be omitted, meaning that value will be fetched from the context
+ *  by the name of the key:
+ *
+ *    x = 0
+ *    y = 100
+ *    h = {x:, y:}
+ *    h # => {:x=>0, :y=>100}
+ *
  *  === Common Uses
  *
  *  You can use a \Hash to give names to objects:
-- 
cgit v1.2.1


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

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