You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: en/news/_posts/2019-12-12-separation-of-positional-and-keyword-arguments-in-ruby-3-0.md
+13Lines changed: 13 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -22,6 +22,7 @@ In most cases, you can avoid the incompatibility by adding the _double splat_ op
22
22
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.
23
23
24
24
## Typical cases
25
+
{: #typical-cases }
25
26
26
27
Here is the most typical case. You can use double splat operator (`**`) to pass keywords instead of a Hash.
27
28
@@ -67,6 +68,7 @@ bar({ k: 42 }) # => {:k=>42}
67
68
{% endhighlight %}
68
69
69
70
## What is deprecated?
71
+
{: #what-is-deprecated }
70
72
71
73
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.
72
74
@@ -95,6 +97,7 @@ foo(k: 1) #=> {:k=>1}
95
97
{% endhighlight %}
96
98
97
99
## Will my code break on Ruby 2.7?
100
+
{: #break-on-ruby-2-7 }
98
101
99
102
A short answer is "maybe not".
100
103
@@ -105,8 +108,10 @@ Except for the warnings and minor changes, Ruby 2.7 attempts to keep the compati
105
108
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.
106
109
107
110
## Handling argument delegation
111
+
{: #delegation }
108
112
109
113
### Ruby 2.6 or prior
114
+
{: #delegation-ruby-2-6-or-prior }
110
115
111
116
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.
112
117
@@ -117,6 +122,7 @@ end
117
122
{% endhighlight %}
118
123
119
124
### Ruby 3
125
+
{: #delegation-ruby-3 }
120
126
121
127
You need to explicitly delegate keyword arguments.
122
128
@@ -135,6 +141,7 @@ end
135
141
{% endhighlight %}
136
142
137
143
### Ruby 2.7
144
+
{: #delegation-ruby-2-7 }
138
145
139
146
In short: use `Module#ruby2_keywords` and delegate `*args, &block`.
140
147
@@ -149,6 +156,7 @@ end
149
156
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.
150
157
151
158
### A compatible delegation that works on Ruby 2.6, 2.7 and Ruby 3
159
+
{: #a-compatible-delegation }
152
160
153
161
In short: use `Module#ruby2_keywords` again.
154
162
@@ -190,10 +198,12 @@ If you really worry about the portability, use `ruby2_keywords`. (Acknowledge t
190
198
`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).
191
199
192
200
## Other minor changes
201
+
{: #other-minor-changes }
193
202
194
203
There are three minor changes about keyword arguments in Ruby 2.7.
195
204
196
205
### 1. Non-Symbol keys are allowed in keyword arguments
206
+
{: #other-minor-changes-non-symbol-keys }
197
207
198
208
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.
199
209
@@ -237,6 +247,7 @@ bar("key" => 42, :sym => 43)
237
247
{% endhighlight %}
238
248
239
249
### 2. Double splat with an empty hash (`**{}`) passes no arguments
250
+
{: #other-minor-changes-empty-hash }
240
251
241
252
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.
242
253
@@ -269,6 +280,7 @@ foo(**empty_hash)
269
280
{% endhighlight %}
270
281
271
282
### 3. The no-keyword-arguments syntax (`**nil`) is introduced
283
+
{: #other-minor-changes-double-splat-nil }
272
284
273
285
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)
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.
0 commit comments