From b00f3fc62d938d2ef9bc360fa2dcd09ff2ac0d57 Mon Sep 17 00:00:00 2001 From: masatoshi_moritsuka Date: Fri, 6 Jan 2023 14:09:10 +0900 Subject: [PATCH] =?UTF-8?q?respond=20to=20=E9=96=A2=E9=80=A3=E3=81=AE?= =?UTF-8?q?=E7=BF=BB=E8=A8=B3=E3=82=92=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit cf. https://github.com/rurema/doctree/pull/2773#issuecomment-1370510488 > respond to `foobar`のような部分は、「foobar **メソッド**を持つオブジェクト」「オブジェクトに foobar メソッドを定義する」と直接的に表現するのはどうだろうか(そして注釈を削除し、respond toの香りを無くす) > * respond toをあまり大事にする必要がないと思うため: > > * トレイトやインターフェイスのないRubyでは、ダックタイピング的に実装することになるが、そこまでくると、直接的に「メソッドを持つ」でいいと感じる > * ココをうまく使いこなすのに、内部の挙動である`Object#respond_to?`まで踏み込んで理解する必要性は薄く(個人的見解)、却って(初心者寄りの読者は特に)混乱を招きそう --- refm/doc/spec/pattern_matching.rd | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/refm/doc/spec/pattern_matching.rd b/refm/doc/spec/pattern_matching.rd index afc66145bd..976adfca98 100644 --- a/refm/doc/spec/pattern_matching.rd +++ b/refm/doc/spec/pattern_matching.rd @@ -109,13 +109,10 @@ users.any? {|user| user in {name: /B/, age: 20..} } #=> true Array/Find/Hash パターンの中に 『』 と書かれている場所では任意のパターンをネストさせることができます。 #@# Array patterns and find patterns match arrays, or objects that respond to +deconstruct+ (see below about the latter). -Array パターン と Find パターン は配列か deconstruct を持つオブジェクトにマッチします。(deconstruct については後ほど説明します) +Array パターン と Find パターン は配列か deconstruct メソッドを持つオブジェクトにマッチします。(deconstruct については後ほど説明します) #@# Hash patterns match hashes, or objects that respond to +deconstruct_keys+ (see below about the latter). Note that only symbol keys are supported for hash patterns. -Hash パターン はハッシュか deconstruct_keys を持つオブジェクトにマッチします。(deconstruct_keys については後ほど説明します) Hash パターン で利用できるキーはシンボルのみです。 - -#@# 原文にないが補足のため追加 -※ deconstruct や deconstruct_keys を扱う際の『〜を持つ』の定義は [[m:Object#respond_to?]] と同様です。 +Hash パターン はハッシュか deconstruct_keys メソッドを持つオブジェクトにマッチします。(deconstruct_keys については後ほど説明します) Hash パターン で利用できるキーはシンボルのみです。 #@# An important difference between array and hash pattern behavior is that arrays match only a _whole_ array: Array パターン と Hash パターン の挙動の重要な違いは Array パターンは配列の 「全ての」 要素がマッチする必要があるということです。