[DOC] Add documentation for hash value omission syntax
This commit is contained in:
parent
1dd10e1892
commit
a0f10a973f
Notes:
git
2021-12-20 01:10:11 +09:00
Merged: https://github.com/ruby/ruby/pull/5244 Merged-By: nobu <nobu@ruby-lang.org>
@ -210,6 +210,24 @@ definition. If a keyword argument is given that the method did not list,
|
|||||||
and the method definition does not accept arbitrary keyword arguments, an
|
and the method definition does not accept arbitrary keyword arguments, an
|
||||||
ArgumentError will be raised.
|
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
|
=== Block Argument
|
||||||
|
|
||||||
The block argument sends a closure from the calling scope to the method.
|
The block argument sends a closure from the calling scope to the method.
|
||||||
|
@ -356,6 +356,14 @@ is equal to
|
|||||||
|
|
||||||
{ :"a 1" => 1, :"b 2" => 2 }
|
{ :"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.
|
See Hash for the methods you may use with a hash.
|
||||||
|
|
||||||
== \Range Literals
|
== \Range Literals
|
||||||
|
8
hash.c
8
hash.c
@ -6701,6 +6701,14 @@ static const rb_data_type_t env_data_type = {
|
|||||||
* # Raises SyntaxError (syntax error, unexpected ':', expecting =>):
|
* # Raises SyntaxError (syntax error, unexpected ':', expecting =>):
|
||||||
* h = {0: 'zero'}
|
* 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
|
* === Common Uses
|
||||||
*
|
*
|
||||||
* You can use a \Hash to give names to objects:
|
* You can use a \Hash to give names to objects:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user