Skip to content

Commit 525e8b2

Browse files
authored
Merge pull request #2365 from mame/add-html-anchors-for-keyword-separation
Add HTML anchors for the article of Ruby 3's keyword argument separation
2 parents 7847e54 + 4af89b3 commit 525e8b2

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

en/news/_posts/2019-12-12-separation-of-positional-and-keyword-arguments-in-ruby-3-0.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ In most cases, you can avoid the incompatibility by adding the _double splat_ op
2222
In Ruby 3, a method delegating all arguments must explicitly delegate keyword arguments in addition to positional arguments. If you want to keep the delegation behavior found in Ruby 2.7 and earlier, use `ruby2_keywords`. See the "Handling argument delegation" section below for more details.
2323

2424
## Typical cases
25+
{: #typical-cases }
2526

2627
Here is the most typical case. You can use double splat operator (`**`) to pass keywords instead of a Hash.
2728

@@ -67,6 +68,7 @@ bar({ k: 42 }) # => {:k=>42}
6768
{% endhighlight %}
6869

6970
## What is deprecated?
71+
{: #what-is-deprecated }
7072

7173
In Ruby 2, keyword arguments can be treated as the last positional Hash argument and a last positional Hash argument can be treated as keyword arguments.
7274

@@ -95,6 +97,7 @@ foo(k: 1) #=> {:k=>1}
9597
{% endhighlight %}
9698

9799
## Will my code break on Ruby 2.7?
100+
{: #break-on-ruby-2-7 }
98101

99102
A short answer is "maybe not".
100103

@@ -105,8 +108,10 @@ Except for the warnings and minor changes, Ruby 2.7 attempts to keep the compati
105108
If you want to disable the deprecation warnings, please use a command-line argument `-W:no-deprecated` or add `Warning[:deprecated] = false` to your code.
106109

107110
## Handling argument delegation
111+
{: #delegation }
108112

109113
### Ruby 2.6 or prior
114+
{: #delegation-ruby-2-6-or-prior }
110115

111116
In Ruby 2, you can write a delegation method by accepting a `*rest` argument and a `&block` argument, and passing the two to the target method. In this behavior, the keyword arguments are also implicitly handled by the automatic conversion between positional and keyword arguments.
112117

@@ -117,6 +122,7 @@ end
117122
{% endhighlight %}
118123

119124
### Ruby 3
125+
{: #delegation-ruby-3 }
120126

121127
You need to explicitly delegate keyword arguments.
122128

@@ -135,6 +141,7 @@ end
135141
{% endhighlight %}
136142

137143
### Ruby 2.7
144+
{: #delegation-ruby-2-7 }
138145

139146
In short: use `Module#ruby2_keywords` and delegate `*args, &block`.
140147

@@ -149,6 +156,7 @@ end
149156
In fact, Ruby 2.7 allows the new style of delegation in many cases. However, there is a known corner case. See the next section.
150157

151158
### A compatible delegation that works on Ruby 2.6, 2.7 and Ruby 3
159+
{: #a-compatible-delegation }
152160

153161
In short: use `Module#ruby2_keywords` again.
154162

@@ -190,10 +198,12 @@ If you really worry about the portability, use `ruby2_keywords`. (Acknowledge t
190198
`ruby2_keywords` might be removed in the future after Ruby 2.6 reaches end-of-life. At that point, we recommend to explicitly delegate keyword arguments (see Ruby 3 code above).
191199

192200
## Other minor changes
201+
{: #other-minor-changes }
193202

194203
There are three minor changes about keyword arguments in Ruby 2.7.
195204

196205
### 1. Non-Symbol keys are allowed in keyword arguments
206+
{: #other-minor-changes-non-symbol-keys }
197207

198208
In Ruby 2.6 or before, only Symbol keys were allowed in keyword arguments. In Ruby 2.7, keyword arguments can use non-Symbol keys.
199209

@@ -237,6 +247,7 @@ bar("key" => 42, :sym => 43)
237247
{% endhighlight %}
238248

239249
### 2. Double splat with an empty hash (`**{}`) passes no arguments
250+
{: #other-minor-changes-empty-hash }
240251

241252
In Ruby 2.6 or before, passing `**empty_hash` passes an empty Hash as a positional argument. In Ruby 2.7 or later, it passes no arguments.
242253

@@ -269,6 +280,7 @@ foo(**empty_hash)
269280
{% endhighlight %}
270281

271282
### 3. The no-keyword-arguments syntax (`**nil`) is introduced
283+
{: #other-minor-changes-double-splat-nil }
272284

273285
You can use `**nil` in a method definition to explicitly mark the method accepts no keyword arguments. Calling such methods with keyword arguments will result in an `ArgumentError`. (This is actually a new feature, not an incompatibility)
274286

@@ -301,6 +313,7 @@ foo(k: 1) #=> ArgumentError: unknown keyword k
301313
{% endhighlight %}
302314

303315
## Why we're deprecating the automatic conversion
316+
{: #why-deprecated }
304317

305318
The automatic conversion initially appeared to be a good idea, and worked well in many cases. However, it had too many corner cases, and we have received many bug reports about the behavior.
306319

0 commit comments

Comments
 (0)