Skip to content

[ja] Translate "Separation of positional and keyword arguments" - #2362

Merged
mame merged 3 commits into
ruby:masterfrom
hachi8833:add_kwarg3_doc
Nov 29, 2021
Merged

[ja] Translate "Separation of positional and keyword arguments"#2362
mame merged 3 commits into
ruby:masterfrom
hachi8833:add_kwarg3_doc

Conversation

@hachi8833

Copy link
Copy Markdown
Contributor

Hello, I'm sending the PR based on an advice from @mame.
Could you please check this?

Thank you,

@hachi8833
hachi8833 requested a review from a team as a code owner January 29, 2020 07:53

@mame mame left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you very much for the Japanese translation! It looks very good to me. I added some minor comments. Could you check them out?


## 概要

Ruby 3.0では、位置引数とキーワード引数が分離されます。Ruby 3.0で変更される振る舞いはRuby 2.7でwarningを表示します。以下のいずれかのwarningが表示される場合は、コードのアップデートが必要です。

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rubyの文脈で日本語で「位置引数」というのは一般的ではないような気がします(そうでもない?)。かといって他によい言葉も思いつかなかったので、やや冗長ですがこの文の上に

この文書では便宜上、必須引数、オプション引数、rest引数、後置引数(つまり、キーワード引数とブロック引数以外の引数)をまとめて「位置引数」と呼びます。

みたいな一文を置くのはどうでしょうか。

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

はい、お任せします!
(ローカル翻訳時は類似の注釈を入れてました)


## Q: 自分のコードはRuby 2.7で動かなくなりますか?

手短かに言うと「壊れない可能性はあります」。

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

「私のコードは壊れますか?」「たぶん壊れないです」なので、見出しを「動かなくなりますか?」にするなら「たぶん動きます」という感じを意図していました。
見出しに「Q:」と付けるなら、それに対応して「A: たぶん動きます。」とかでもよいかもしれません。


手短かに言うと「壊れない可能性はあります」。

Ruby 2.7におけるこの変更は、3.0への移行パスとして設計されています。あくまで原則としてですが、Ruby 2.7ではRuby 3.0で変更される振る舞いについてwarningを出すにとどめており、warningの中には私たちが微細とみなしている変更点も若干含まれます。詳しくは後述の「その他の微細な変更点」をご覧ください。

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


Ruby 2.7では、原則として、Ruby 3.0で変更される振る舞いについてwarningを出すにとどめています。しかし、私たちが軽微とみなした非互換も少しだけ入っています。

という感じです。


## 概要

Ruby 3.0では、位置引数とキーワード引数が分離されます。Ruby 3.0で変更される振る舞いはRuby 2.7でwarningを表示します。以下のいずれかのwarningが表示される場合は、コードのアップデートが必要です。

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

そういえば、warningを「警告」と訳してないのはなんか意図があります?(個人的にはどちらでもよいです)

end
{% endhighlight %}

残念ながら、Ruby 2.6以前では新しい委譲スタイルを正しく扱えないため、旧来の委譲スタイル(`**kwargs`を使わないなど)を使う必要があります。これは、キーワード引数を分離した理由のひとつでもあります(詳しくは本記事末尾をご覧ください)。`ruby2_keywords`を用いれば、Ruby 2.7や3.0でも旧来の委譲スタイルを引き続き利用できます。2.6以前のRubyでは`ruby2_keywords`が定義されていないので、[ruby2_keywords](https://rubygems.org/gems/ruby2_keywords) gemを使うか、以下を手動で定義します。

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i.e., は「など」ではなく「つまり」です。

旧来の委譲スタイル(つまり、**kwargsを受け渡ししないスタイル)を使う必要があります。

とか。


## その他の微細な変更点

Ruby 2.7のキーワード引数では、この他に以下の3つのマイナーチェンジが行われています。

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ここだけ「マイナーチェンジ」がカタカナなのは意図的?(どっちでもいいです)


### 3\. キーワード引数を受け取らないことを表す構文(`**nil`)が導入される

メソッド定義で`**nil`を用いることで、そのメソッドがキーワード引数を受け取らないことを明示的に示せるようになります。このメソッドを呼び出すときにキーワード引数を渡すと`ArgumentError`が表示されます(これは非互換性ではなく、事実上新機能です)。

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

事実上というか本当に新機能なので、「事実上」はいらないかも。

#=> Ruby 2.7以降: no keywords accepted (ArgumentError)
{% endhighlight %}

この新構文は、メソッドがキーワード引数を受け取らないことを明示的に指定するのに有用です。これを使わない場合、キーワード引数は上述の例の他の引数に吸い込まれます。メソッドを拡張してキーワード引数を受け取るようにする場合、以下のような非互換性が発生する可能性があります。

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

「他の引数」がわかりにくい気がしたので「rest引数」がよいかも。


当初、自動変換はうまいアイデアに思われていて、多くの場合問題なく機能していました。しかし、エッジケースがあまりにも多く、これまでこの振る舞いに関するバグレポートを山のように受け取りました。

自動変換は、オプションの位置引数とキーワード引数をどちらも受け取るメソッドではうまく動きません。末尾のHashオブジェクトを位置引数として扱うことを期待する人々もいれば、末尾のHashオブジェクトをキーワード引数として扱うことを期待する人々もいました。

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

「オプションの位置引数」→「オプション引数」だけでよいかも。

#=> Ruby 2.7以降: []
{% endhighlight %}

`foo()`には引数がありませんが、Ruby 2.6では空のハッシュ引数が`target`に渡されます。理由は、メソッド`foo`が明示的にキーワード(`**kwargs`)を委譲しているためです。`foo()`が呼び出されると、`args`は空のArrayになり、`kwargs`は空のHashになります。そして`target(*args, **kwargs, &block)`は空のHashを引数として1つ渡します。理由は、`**kwargs`が自動的にHash位置引数に変換されるためです。

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

まったく重要じゃないんですが、 "and block is nil" が訳から漏れているのに気づいてしまったので一応

foo()が呼び出されると、argsは空のArrayになり、kwargsは空のHashになり、blocknilになります。


上のコードの最下部に書いたように、`**{}`を渡すことでこの問題を回避できます。

移植性がどうしても不安な場合は`ruby2_keywords`をお使いください(Ruby 2.6以前ではキーワード引数周りで膨大なエッジケースが存在していることを知っておいてください)。`ruby2_keywords`は、今後Ruby 2.6が役目を終えたときに削除される可能性があります。現時点で私たちがおすすめできるのは、キーワード引数を明示的に委譲することです(上述のRuby 3向けのコードを参照)。

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At this timeではなくAt that timeなので

現時点で私たちがおすすめできるのは、キーワード引数を明示的に委譲することです

ではなくて、

そのときになったら、キーワード引数を明示的に委譲することをおすすめします

という意図でした。(英語のほうが正しく意図を表現できていないような気もします)

@jinroq jinroq mentioned this pull request Nov 27, 2021
mame added a commit that referenced this pull request Nov 29, 2021
@mame
mame merged commit 14e04bc into ruby:master Nov 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants