Skip to content

Commit f0931fb

Browse files
znzclaude
andcommitted
String#insert: レビュー指摘を反映して説明を整理
PR #2925 への対応。znz のレビュー指摘を反映: - 最初の段落は要約のみとし、pos が正/負の場合の説明を後段に分けた - 正の場合は self[pos, 0] = other と同じ操作である旨を明記 - 削除されていた @see [[m:String#[]=]] を復活 - 例を PR 説明のとおり充実させ、正/負それぞれで insert と []= の 異同が分かるようにした(出力は Ruby 3.4 で確認済み) Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent ea28370 commit f0931fb

1 file changed

Lines changed: 11 additions & 7 deletions

File tree

refm/api/src/_builtin/String

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1815,23 +1815,27 @@ offset が負の場合、文字列の末尾から数えた位置から探索し
18151815
--- insert(pos, other) -> self
18161816

18171817
文字列 other を self に挿入して self を返します。
1818-
pos が正の場合、other を開始インデックスに挿入します。
18191818

1820-
#@samplecode 例
1821-
'foo'.insert(1, 'bar') # => "fbaroo"
1822-
#@end
1819+
pos が正の場合、pos 番目の文字の直前に文字列 other を挿入します。
1820+
self[pos, 0] = other と同じ操作です。
18231821

18241822
pos が負の場合、self の末尾から逆方向に数えて pos+1 (self[index] の後) に other を挿入します。
1825-
pos 番目の文字の直前に文字列 other を挿入します。
1826-
self[pos, 0] = other と同じ操作です。
18271823

18281824
@param pos 文字列を挿入するインデックス
18291825
@param other 挿入する文字列
18301826

18311827
#@samplecode 例
1832-
'foo'.insert(-2, 'bar') # => "fobaro"
1828+
# pos が正の場合、String#insert と String#[]= は同じ操作を行う
1829+
'foo'.insert(1, 'bar') # => "fbaroo"
1830+
str = 'foo'; str[1, 0] = 'bar'; str # => "fbaroo"
1831+
1832+
# pos が負の場合、String#insert と String#[]= は異なる操作を行う
1833+
'foo'.insert(-1, 'bar') # => "foobar"
1834+
str = 'foo'; str[-1, 0] = 'bar'; str # => "fobaro"
18331835
#@end
18341836

1837+
@see [[m:String#[]=]]
1838+
18351839
--- intern -> Symbol
18361840
--- to_sym -> Symbol
18371841

0 commit comments

Comments
 (0)